Home >>PHP Tutorial >echo and print in PHP

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.

echo

  1. echo is a statement i.e used to display the output. it can be used with parentheses echo or without parentheses echo.
  2. echo can pass multiple string separated as ( , )
  3. echo doesn't return any value
  4. echo is faster then print

 

For Example

<?php
$name="John";
echo $name;
//or        
echo ($name);
?>
Output John

  In the above example Create and initialize variable($name) hold a string value="John". We want to print the name for this ($name) variable declare inside the echo with or without parentheses . It will display the same output.  

For Example  (pass multiple argument)

<?php
$name = "John";
$profile = "PHP Developer";
$age = 25;
echo $name , $profile , $age, " years old";
?>
Output John PHP Developer 25 years old

In the above example $name , $profile and $age are three variable with value=("John" , "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  

For Example (check return type)

<?php
$name = "John";
$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="John". now we check the return type. when we run the program it show error,because echo has no return type.  

Print

  1. Print is also a statement i.e used to display the output. it can be used with parentheses print( ) or without parentheses print.
  2. using print can doesn't pass multiple argument
  3. print always return 1
  4. it is slower than echo

 

For Example

<?php
$name="John";
print $name;
//or
print ($name);
?>
Output John

In the above example Declare a variable ($name) value="John". now we want to print the name. we simply define $name inside print statement with or without parentheses. it will show the output: "John" .

For Example (pass multiple argument)

<?php
$name = "John";
$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("John","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 .

 For Example (check return type)

<?php
$name = "John";
$ret =  print $name;
//To test it returns or not
echo $ret;
?>
Output John

In the above example declare a variable $name hold value="John".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 Sidebar ads