Which statement should I use in php scripts? Echo or Print? What is faster and mostly used? Thanks in advance.
9 Answers
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.
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.
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.
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.

- 1,955
- 1
- 24
- 29
-
1+1 for the detailed differences, but if you're going to use the ternary operator, use it correctly: `= 18) ? 'Can vote.' : 'Cannot vote.'); ?>`. – Andrew Larsson Oct 03 '13 at 16:45
-
1In fact, my example is correct for the purpose. My point was to illustrate code which would work for print, but not for echo, not to show the best or most correct usage of the ternary operator. Your "correction" does not serve the purpose: it works for both print and echo. I hope that now you understand my example better. – Tripartio Oct 07 '13 at 16:10
-
-
You can use echo with ternary operators. = 18)? 'can vote.':'cannot vote.'; ?> – Aug 07 '14 at 18:25
-
1The example you gave in your comment (as in the previous comment) is different from my example. My example uses print as the result value of the ternary operator, not as the evaluating expression. My point is that echo does not work as a result value, because it does not return a value. – Tripartio Aug 07 '14 at 22:43
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.
-
1Feel free to edit the answer with appropriate updates - the question was answered a long time ago. – Ryan Jul 13 '13 at 20:43
-
1It isn't very meaningful to say that echo offers `=$foo?>` (or `` as of PHP 5.4). That isn't an "echo"--it's just an alternate PHP language construct that does the same thing as echo or print. You could just as well say that `=$foo?>` is "print" shortcut syntax. – Tripartio Oct 07 '13 at 20:19
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
}

- 10,861
- 13
- 46
- 72

- 1,246
- 2
- 16
- 22
-
-
this is the so much better. do syntax highlighters usually grab print() statements? – expiredninja Jun 19 '14 at 00:08
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

- 1,666
- 1
- 17
- 30
echo is faster. also echo() can print more than 1 argument, print() can only print 1 argument.

- 17,518
- 14
- 92
- 123
echo
can have two parameters
Ex: echo $x, "\n";
print
only one parameter
Ex: print "\n";

- 31
- 5
echo()
is faster than print()
in every usage case I can think of - not by much, but it adds up.
Here's some benchmarking proof: http://www.phpbench.com scroll down to "echo vs. print"

- 76,997
- 17
- 122
- 145
The fastest way to echo out multiple strings is:
echo "string","string","string";
This doesn't even need to perform a concatenation of the string, it just echoes one string after another. There is nothing wrong with print()
, and I think at the end of the day unless you are echoing out millions of lines, you wouldn't really see a difference.

- 6,538
- 5
- 47
- 64
IMO this is not a question of speed. If I was developing an application where the speed difference between echo and print is of any significance I would consider that a very clear sign to use a different (not interpreted) programming language than PHP.
But as long as you stick to PHP it is - IMvHO - simply a question of flavor. And btw., I prefer echo, but just because it means less typing ;)

- 13,402
- 1
- 21
- 15