Search This Blog

Friday, January 8, 2016

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
Eg i
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)
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)
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

  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
Eg i
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)
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)
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