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, 5:57 AM EST
nice
Santhakumar MunuswamyPosted Aug 15, 2015, 10:35 PM EDT
Nice Article. Thanks for sharing
Sandeep KumarPosted Aug 14, 2015, 3:56 AM EDT
Thank you rahul sir :)
Sandeep KumarPosted Aug 14, 2015, 3:55 AM EDT
Thank You Gopi Chand sir :)
Rahul PrajapatPosted Aug 14, 2015, 3:55 AM EDT
Nice article sandeep
Gopi ChandPosted Aug 14, 2015, 3:37 AM EDT
Good work
Sandeep KumarPosted Aug 14, 2015, 3:15 AM EDT
Thank you pankaj sir for appreciate and your suggestions :)
Pankaj Kumar ChoudharyTOP 100Posted Aug 14, 2015, 3:07 AM EDT
Nice Article Sandeep............
Sandeep KumarPosted Aug 14, 2015, 3:03 AM EDT
Thank you Mohammed Ibrahim sir :)
Sandeep KumarPosted Aug 14, 2015, 3:02 AM EDT
thank you Karthikeyan K sir :)
Sandeep KumarPosted Aug 14, 2015, 3:01 AM EDT
Thank you Rakesh Chavda sir:)
Sandeep KumarPosted Aug 14, 2015, 3:00 AM EDT
Thank you Neeraj Kumar :)
Sandeep KumarPosted Aug 14, 2015, 3:00 AM EDT
Thank you Vaikesh K P sir :)
Sandeep KumarPosted Aug 14, 2015, 2:59 AM EDT
Thank you Ankit Bansal sir :)
Sandeep KumarPosted Aug 14, 2015, 2:58 AM EDT
Thank you Sibeesh Venu sir :)
Sibeesh VenuTOP 100Posted Aug 14, 2015, 2:56 AM EDT
Nice Share :)
Ankit BansalPosted Aug 13, 2015, 10:59 PM EDT
nice..
Vaikesh K PPosted Aug 13, 2015, 9:25 PM EDT
Nice
Neeraj KumarPosted Aug 13, 2015, 8:25 PM EDT
GOod
RakeshPosted Aug 13, 2015, 8:16 PM EDT
Good one
Mohammed IbrahimPosted Aug 13, 2015, 8:11 PM EDT
gud work
Karthikeyan KPosted Aug 13, 2015, 7:32 PM EDT
Good one...Thanks for sharing