Pages

Wednesday, June 28, 2017

Difference between echo and print in PHP

Difference between echo and print in PHP



PHP echo and print both are PHP Statement.

Both are used to display the output in PHP.
Difference Between Echo And Print In PHP
echo
  • echo is a statement i.e used to display the output. it can be used with parentheses echo( ) or without parentheses echo.
  • echo is faster then print
  • Using echo can pass multiple string seperated as ( , )
  • echo doesn�t return any value
Eg i
 <?php
$name="Ravi";
echo $name;
//or
echo ($name);
?>

 Output: Ravi

In the above example
Create and initialize variable($name) hold a string value=�Ravi�. we want to print the name for this ($name) variable declare inside the echo with or without parentheses .
it will display the same output.
Eg ii (pass multiple argument)
 <?php
$name = "Ravi ";
$profile = "PHP Developer";
$age = 25;
echo $name , $profile , $age, " years old";
?>

 Output: Ravi PHP Developer25 years old

In the above example
$name , $profile and $age are three variable with value=(�ravi� , �php developer� and 25) respectively.
now we want to print all three variable values together. all variables names are define inside the echo statement separated by comm or dot(, or .)
it will show the output
Eg iii (check return type)
 <?php
$name = "Ravi ";
$ret = echo $name;
?>

 Output: Parse error: syntax error, unexpected T_ECHO

In the above example
In this program we check the return type of �echo�. declare a variable $name with value=�ravi�.
now we check the return type. when we run the program it show error,because echo has no return type.
print
  • print is also a statement i.e used to display the output. it can be used with parentheses print( ) or without parentheses print.
  • it is slower than echo
  • using print can doesn�t pass multiple argument
  • print always return 1
Eg i
 <?php
$name="Ravi";
print $name;
//or
print ($name);
?>

 Output: Ravi

In the above example
Declare a variable ($name) value=�ravi�. now we want to print the name. we simply define $name inside print statement with or without parentheses.
it will show the output: �ravi� .
Eg ii (pass multiple argument)
 <?php
$name = "Ravi ";
$profile = "PHP Developer";
$age = 25;
print $name , $profile , $age, " years old";
?>

 Output: Parse error: syntax error
In the above example
Declare three variable $name, $profile, $age and hold the value(�ravi�,�php developer�,25).
now check whether it will allow execute multiple argument. Pass three variable inside the print statement separated by comma. as we run this program it show some error.
it means multiple argument are not allow in print .
Eg iii (check return type)
 <?php
$name = "Ravi ";
$ret = print $name;
//To test it returns or not
echo $ret;
?>

 Output: Ravi 
In the above example
declare a variable $name hold value=�ravi�.now we check the return type of print .
So (print $name )is store in a variable($ret) .
it will show $name value with return type=1.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.