First, let me state that print has not been deprecated in favor of echo. At least I couldn’t find any evidence of it. (You know who you are.)
There are two clear differences between print and echo in php.
- print always returns 1, echo does not return anything
This means that print can be used in expressions, where echo cannot:if (print "hi" == 1) { print "print returned 1"; }
- echo concatenates arguments, print does not
This means you can use commas instead of periods for concatenation:$enthusiasm = array('.', '!', '!!', '!!!'); echo "Hello", "," . " ", "world", $enthusiasm[1];
Note that this means print can produce undesired results in expressions if you expect the parentheses to take precedence, as they do with a function call. See this comment on the php.net reference documentation.
See also this post about the differences between print and echo:
http://www.faqts.com/knowledge_base/view.phtml/aid/1/fid/40
Also see this comment about performance differences:
http://www.php.net/manual/en/function.print.php#66392
Short answer, the performance difference is negligible.
An interesting experiment might be to compare argument based concatenation in echo with the concatentation operator in print:
<? #hidad.php ob_start(); for($i=0; $i<$argv[1]; $i++) { echo 'Hi ', 'dad'; } ob_end_clean(); ?> <? #himom.php ob_start(); for($i=0; $i<$argv[1]; $i++) { print 'Hi '. 'mom'; } ob_end_clean(); ?>
Results
c:\temp> time php himom.php 1000000 real 0m1.344s user 0m0.046s sys 0m0.000s c:\temp> time php himom.php 1000000 real 0m1.306s user 0m0.015s sys 0m0.031s c:\temp> time php himom.php 1000000 real 0m1.318s user 0m0.000s sys 0m0.046s c:\temp> time php hidad.php 1000000 real 0m1.215s user 0m0.015s sys 0m0.031s c:\temp> time php hidad.php 1000000 real 0m1.180s user 0m0.000s sys 0m0.031s c:\temp> time php hidad.php 1000000 real 0m1.242s user 0m0.015s sys 0m0.015s
It looks like there is a slight edge to echo argument concatenation, at least with my limited sample on Windows Vista (using time via Cygwin for benchmarking.) echo also beat out print with a concatenation operator by a smaller margin, but the differences are too slight to be definitive.
I used output buffering to remove IO from the equation. I used ob_end_clean() instead of ob_flush() to avoid printing out the results, though we might still be primarily measuring the efficiency of deleting the buffer.
I tried a smaller result set (10000 vs. 1000000) and got comparable measurements, of about 1/3 the duration, which I’d guess means that most of that time is spent initializing the php interpreter.
c:\temp> time php hidad.php 10000 real 0m0.337s user 0m0.015s sys 0m0.047s c:\temp> time php himom.php 10000 real 0m0.342s user 0m0.000s sys 0m0.062s c:\temp> time php himom.php real 0m0.315s user 0m0.031s sys 0m0.015s c:\temp> time php hidad.php real 0m0.330s user 0m0.000s sys 0m0.046s
Also note that I modified my script to use a static iteration number (1000000) instead of a variable ($argv[1]) .and got significantly greater speed improvement than the difference between using print and echo or the difference between dot concatenation and comma (argument) concatenation.
<? #himom.php ob_start(); for($i=0; $i<1000000; $i++) { print 'Hi '. 'mom'; } ob_end_clean(); ?>
c:\temp>time php himom.php real 0m1.055s user 0m0.000s sys 0m0.062s c:\temp>time php hidad.php real 0m0.969s user 0m0.015s sys 0m0.015s
Finally, even the difference between printing the text “Hi mom” and “Hi dad” appears slightly different, but now we’re just getting lost in noise.