PHP Programming

PHP: Echo Versus Print(f)

One of the things I remember about when I started programming in PHP was the confusion I ran in to when deciding how to print text.  Should I use the print construct?  Or maybe I should use the echo construct?  Perhaps the C-style printf function instead?  Eventually most people just latch on to the echo construct because “That’s what everyone else is doing”.  Is that the fastest method to use though? Well, that’s what I’m going to find out.

The Test

The test I decided to use loops 1000 times, and inside the main loop, there is a sub-loop creating the output which runs from 0 to N (where N is the value of the outer-loop counter).

$start = microtime(true);
for($i = 0; $i < 1000; $i++) {
     for($x = 0; $x < $i; $x++) {
          //Output here.
$end = microtime(true);

After the test has been completed, I just wrote the times to a text file for collection later.

The Results

  • echo – 16.470 seconds
  • print – 16.473 seconds
  • printf – 16.432 seconds

As I expected, the difference between print and echo were negligible.  I suspect that they use the same underlying code.  The real surprise was from printf(), which is a function and is supposed to have taken longer because of the overhead of calling it.  I’m at a loss as to why it’s faster than the other two, but the difference isn’t really significant enough to worry about.  If someone wanted to be a bit more scientific about this, this program should be run probably a couple thousand times so that we can get a good sample and see what happens then.

If you would like to run this code yourself and let me know your results, it’s available for download here.

By Jack Slingerland

Founder of Working and living in Raleigh, NC. I manage a team of software engineers and work in Python, Django, TypeScript, Node.js, React+Redux, Angular, and PHP. I enjoy hanging out with my wife and son, lifting weights, and advancing in my free time.

4 replies on “PHP: Echo Versus Print(f)”

When you report benchmarks, it’s a good idea to tell what version of PHP you used, what operating system, environment, etc.

I tried your test while redirecting the output to /dev/null to minimize the overhead of the I/O.
I’m using PHP 5.3.3 on a Macbook with OS X 10.5.8.
echo – 1.75 sec
print – 1.75 sec
printf – 3.00 sec

I also tried modifying the script to use output buffering (wrap each test with ob_start() / ob_end_flush()), and I got these different results:
echo – 0.46 sec
print – 0.46 sec
printf – 1.28 sec

This shows that at least in this test, there is overhead from doing many small prints. Reducing this overhead by consolidating output using output buffering can have more benefit than the micro-optimizing choice between echo and print. Also printf does have some performance cost, so it’s worth using it only when you need its formatting control.

Good point about reporting the system information (PHP 5.3.3, Macbook Pro, OS X 10.6.4). The results of your tests are more along the lines of what I had expected in this testing. Thanks for running your tests and posting your results.

I noticed in your script that you do the three prints sequentially. I know in C++ that if you’re doing benchmarking and such it’s a good idea to run them individually or run a few thousand runs before beginning the test for time to make sure the startup processes aren’t slowing down the initial runthrough which would be echo in this case. What do you get if you run them backwards? ie: printf(), print(), then echo()?

Comments are closed.