Should I use echo or print in php scripts?

I tested it myself:

$StartTime=microtime(1);
echo '<div style="display:none">';
for($i=0;$i<100000;$i++)
    echo "Hello world!<br />";

echo "</div>Echo: ".round(microtime(1)-$StartTime,5);

$StartTime=microtime(1);
echo '<div style="display:none">';
for($i=0;$i<100000;$i++)
    print "Hello world!<br />";

echo "</div><br />Print: ".round(microtime(1)-$StartTime,5);

echo is around .09s
print is around .3s to .5s


It doesn't affect the way the text is displayed, but both have different behaviour...

For example, print returns a value (true or false) depending on if it can or can't display the text to print; instead, echo simply gives and goes on.

It is valid to do things like this:

if (print ($variable)) {
//do something
}

This is meaningless:

if (echo $variable) {
//do something
}

Both echo and print are language constructs of PHP (not functions). Which is better depends on your priorities. There are three possible priorities I would consider: 1. you mentioned speed; 2. you mentioned widespread usage; 3. I would add flexibility.

  1. Speed: as many others have mentioned, echo is slightly faster (especially when using the multiple-argument syntax, with elements separated by commas), but the difference is so minor that it only matters in code with thousands of loops where speed really, really matters. See http://www.phpbench.com for benchmarks.

  2. Widespread usage: it seems that out of tradition, echo is more popularly used for PHP than is print. This is quite anecdotal, but I think you'll run into the same conclusion when you read PHP code from a wide variety of sources.

  3. Flexibility: I believe print is definitely more flexible than echo in expressing code. Echo has only one "advantage" over print: you can use the following syntax: echo $arg1, $arg2, ... using commas to list your arguments; print does not support the comma syntax. However, you can replace the commas with periods (.) and get exactly the same result in both echo and print: print $arg1. $arg2. .... Thus, this syntax provides zero advantage in flexibility and expression. It is a slight advantage because it results in faster code, as I mentioned in #1, but in 99% of code, this probably doesn't matter.

    In contrast, the one thing that print can do that echo cannot do is to return a value, and so it can fully act as a function. On one hand, it is limited because print always returns a value of 1, no matter what. On the other hand, you can do this with print, but not with echo:

    <?php ($age >= 18) ? print('Can vote.') : print('Cannot vote.'); ?>

    (Example taken from Murach's PHP and MySQL 2010, p. 227)

    Thus, print can express virtually all the same flexibility in code as echo, but echo has one significant use case where it cannot do what print can do: print can act as a function in contexts where this might be useful. (I say "act as a function" because it is not a function; it is a language construct, just like echo.)

    As for the shorthand echo syntax <?=$foo?> (<?php=$foo?> also works as of PHP 5.4: http://us2.php.net/manual/en/function.echo.php), it might be called a shorthand for "echo", but you could just as well call it a shorthand for "print" because it's just a different language construct. There is no logical basis of calling this an "advantage" of echo over print, as some people claim, since this construct is neither an echo nor a print--it is an alternate construct that does the same thing as both.

For me personally, I prefer to pick one and stick with it always. I personally prefer print because of its slightly superior advantage in coding flexibility, and because "print" sounds more intuitive to me--this is purely subjective. I don't care that echo is probably more widely used, because print is equally well understood if others need to read my code. For the 1% of code where print speed really, really matters, then I'll use echo.


Supposedly echo is faster, but either one would work just fine.

Echo also offers a shortcut syntax when embedding php in HTML. i.e.

    I have <?=$foo?> foo.

vs

     I have <?php echo $foo;?> foo.

See http://us2.php.net/manual/en/function.echo.php

Tags:

Php

Printing

Echo