PHP provides the following methods for printing data in the browser:
- print()
- echo()
- printf()
- sprintf()
print(): this statement simply outputs to the browser. It's prototype looks as in:
int print(argument)
There are different typography for print() as in the following:
- <?php print("Be a part of c-sharpcorner");?>
- <?php
- $var="Be a part of c-sharpcorner";
- print "$var";
- ?>
Both of the preceding statements provide the same output, which is:
Be a part of c-sharpcorner
Technically it is not a function, it's a language construct. It always returns 1 regardless of the output.
echo(): This is an alternative option to the print() statement. It just outputs the data to the browser without returning anything, like print(), and that is the main difference between print() and echo().
echo() is capable of outputting multiple strings.
It's prototype can be written in the following manner:
- void echo(string argument1,string argument2,….,string argument n);
Example
- <?php
- echo “Be a part of c-sharpcorner” ;
- ?>
Example
- <?php
- $var1="hello";
- $var2="how are ";
- $var3="you? ";
- echo $var1, " Mr. ", $var2,$var3;
- ?>
The output is:
Hello Mr . how are you?
Note: Here the question develops of which is faster, echo() or print()?
The answer is echo() is faster than print() because print returns a value whereas echo doesn't. This difference can't be seen because we can't examine such a small speed difference.
printf(): If we want to get output that is a combination of both text and dynamic information, in other words a variable.
It automatically separates both the static data and dynamic content.
It is useful because it provides user control on the variable information that will be displayed in the terms of type, precision, alignment and position.
It provides the extensibility as we used in C programming.
The number of type specifiers should be equal to or less than the number of their respective values.
It's prototype looks as in:
- integer printf(string format [, mixed arguments])
Example
- <?php printf("Hello I got %d marks in maths and %d marks in hindi and got %.2f percentage in 10th class",97,86,85);?>
The output will be:
Hello I got 97 marks in maths and 86 marks in hindi and got 85.00. percentage in 10th class.
Table for common type specifiers:
Type |
Description |
%c |
Argument considered an integer; presented as a character corresponding to that ASCII value |
%d |
Argument considered an integer; presented as a signed decimal number |
%f |
Argument considered a floating-point number; presented as a floating-point number |
%o |
Argument considered an integer; presented as an octal number |
%s |
Argument considered a string; presented as a string |
%u |
Argument considered an integer; presented as an unsigned decimal number |
%x |
Argument considered an integer; presented as a lowercase hexadecimal number |
%X |
Argument considered an integer; presented as an uppercase hexadecimal number |
%b |
Argument considered an integer; presented as a binary number |
sprint(): It's similar to printf() except that In printf() we directly get the output whereas sprint provides the output as a string. It's prototype can be written as:
- string sprintf(string format , mixed arguments);
Example
- <?php
- $he=sprintf("%d",23.76);
- echo "$he $he";
- ?>
The output will be:
23 23
Thank you for your patience and reading this article.