PHP: Differences Between echo
and print
Both echo
and print
are PHP language constructs (and not exactly functions), and can be used without parentheses.
There are a few differences between the two and we note them in the sections below.
1) echo
accepts multiple comma-separated arguments, print
only 1
echo
can take several arguments (as illustrated below)
<?php
echo 'Hello', ' ', 'Angni', ' ', 'Sona'; // Hello Angni Sona
?>
whereas print
can accept only one, and the below script returns an error
<?php
print 'Hello', ' ', 'Angni', ' ', 'Sona'; // error
?>
But even for echo
, it works only without parentheses. Passing comma-separated strings as arguments into the parentheses (as shown below) will result in an error
<?php
echo('Hello', ' ', 'Angni', ' ', 'Sona'); // error
?>
2) print
returns 1, echo
none
print
has a return value 1, echo
has none. It can be checked by executing the below script. Since print
always returns 1, we can echo it
<?php
echo print(''); // 1
?>
whereas the below line results in an error, as echo
does not have a return value
<?php
echo echo(''); // error
?>
3) echo
is faster than print
- in microseconds (μs)!
There are some bench mark tests/results available for echo
v. print
, and echo
is slightly faster than print
. But the differences are always in microseconds (μs). I have listed two links below where you may find the comparisons