What is the difference between echo and print with examples
What is the difference between echo and print with examples
echo
- echo statement is used to display the output. echo can be used with parentheses or without parentheses.
- We can pass multiple strings with echo and all of these must separate with commas(,).
- echo doesn’t return any value
- When we print with echo, the operation is fast as compared to print.
echo statement is used to display the output. echo can be used with parentheses or without parentheses.
Example:
<?php
$name=”shahzeb”; echo $name; //or we can echo it as follows; echo ($name); ?> |
Output
shahzeb
We can pass multiple string with echo and all of these must separat with comman(,).
<?php
$name = ” shahzeb “; $profile = “Programmer”; $age = 40; echo $name , $profile , $age, ” years old”; ?> |
Output
shahzeb Programmer 40 years old
echo doesn’t return any value
<?php
$name = ” shahzeb “; $ret = echo $name; ?> |
Output
Parse error: syntax error, unexpected T_ECHO
- echo is faster than print
- print statement is used to display the output. print can be used with parentheses or without parentheses.
- using print can doesn’t pass multiple arguments
- print always return 1
- When we print with print, the operation is slow as compared to echo.
Print is also a statement i.e used to display the output. it can be used with parentheses print( ) or without parentheses print.
<?php
$name=” shahzeb “; print $name; //or we can also print it with echo. print ($name); ?> |
Output
shahzeb
using print can doesn’t pass multiple argument
<?php
$name = ” shahzeb “; $profile = “programmer”; $age = 40; print $name , $profile , $age, ” years old”; ?> |
Output
Parse error: syntax error
print always return 1
<?php
$name = ” shahzeb “; $ret = print $name; //To test it returns or not echo $ret; ?> |
Output
shahzeb
it is slower than echo.