Home >>PHP Tutorial >echo and print in PHP
PHP echo and print both are PHP Statement. Both are used to display the output in PHP.
<?php $name="John"; echo $name; //or echo ($name); ?>
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"; ?>
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; ?>
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.
<?php $name="John"; print $name; //or print ($name); ?>
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"; ?>
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; ?>
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.