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.
| 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.

Asfend YarPosted Feb 29, 2016, 10:57 AM
nice
Santhakumar MunuswamyPosted Aug 16, 2015, 2:35 AM
Nice Article. Thanks for sharing
Sandeep KumarPosted Aug 14, 2015, 7:56 AM
Thank you rahul sir :)
Sandeep KumarPosted Aug 14, 2015, 7:55 AM
Thank You Gopi Chand sir :)
Rahul PrajapatPosted Aug 14, 2015, 7:55 AM
Nice article sandeep
Gopi ChandPosted Aug 14, 2015, 7:37 AM
Good work
Sandeep KumarPosted Aug 14, 2015, 7:15 AM
Thank you pankaj sir for appreciate and your suggestions :)
Pankaj Kumar ChoudharyPosted Aug 14, 2015, 7:07 AM
Nice Article Sandeep............
Sandeep KumarPosted Aug 14, 2015, 7:03 AM
Thank you Mohammed Ibrahim sir :)
Sandeep KumarPosted Aug 14, 2015, 7:02 AM
thank you Karthikeyan K sir :)
Sandeep KumarPosted Aug 14, 2015, 7:01 AM
Thank you Rakesh Chavda sir:)
Sandeep KumarPosted Aug 14, 2015, 7:00 AM
Thank you Neeraj Kumar :)
Sandeep KumarPosted Aug 14, 2015, 7:00 AM
Thank you Vaikesh K P sir :)
Sandeep KumarPosted Aug 14, 2015, 6:59 AM
Thank you Ankit Bansal sir :)
Sandeep KumarPosted Aug 14, 2015, 6:58 AM
Thank you Sibeesh Venu sir :)
Sibeesh VenuPosted Aug 14, 2015, 6:56 AM
Nice Share :)
Ankit BansalPosted Aug 14, 2015, 2:59 AM
nice..
Vaikesh K PPosted Aug 14, 2015, 1:25 AM
Nice
Neeraj KumarPosted Aug 14, 2015, 12:25 AM
GOod
RakeshPosted Aug 14, 2015, 12:16 AM
Good one
Mohammed IbrahimPosted Aug 14, 2015, 12:11 AM
gud work
Karthikeyan KPosted Aug 13, 2015, 11:32 PM
Good one...Thanks for sharing