Elliott C. Back: Internet & Technology

PHP Performance: Echo & Print

Posted in Code,Performance by Elliott Back on October 19th, 2006.

There’s always been big debate about the use of echo versus print in PHP, but it’s time to settle the score. There are three major issues that distinguish echo from print:

  1. Performance characteristics–which is faster?
  2. Return value semantics–print returns 1, while echo does not return anything
  3. Language constructs: neither echo nor print are functions

Echo is faster than print

I wrote some simple code to benchmark echo and print against each other across various iterations of a medium-sized string. You can find the PHP code at the end of the article. Here’s how it breaks down:

  • echo: 463ms over {216 + … + 21} iterations
  • print: 574ms over {216 + … + 21} iterations

The winner: echo is roughly 20% faster than print.

Print can be used as an expression

I’m not sure why you would ever want to do this, but if you did, you’d have to use print. For example, in PHP it’s perfectly legal to write something along the lines:

$a += 1 + print "hello";

This is equivalent of simply incrementing by two, but with an output side effect. Since this is probably a horrible idea, echo wins again here–print‘s return value of 1 shouldn’t be abused.

Print is roughly echo

Did you know that print is not a function? Did you know that echo is not a function? That’s right–they’re both language constructs that mirror each other except for their return value semantics and that print will format its expression list with a \n line terminator if you pass it a list of more than 1 output argument. Therefore, print is slightly heavier than echo.

Appendix A:

<?php

    function _echo_sq($q){
        for($i = 0; $i < $q; $i++)
            echo 'Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Fusce tempor. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Donec elementum augue quis orci. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos hymenaeos. Nam volutpat, nunc vel interdum bibendum, tellus felis nonummy arcu, vitae pretium quam metus quis turpis. Suspendisse risus lacus, pretium bibendum, tincidunt eget, malesuada ac, risus. Etiam ullamcorper erat et magna. Duis blandit mattis est. Maecenas suscipit, quam nec pretium convallis, nisi magna scelerisque est, sit amet lobortis justo est sed ipsum. Aenean viverra, felis ut commodo lobortis, ligula enim laoreet pede, ac tristique nisl magna quis orci. Pellentesque feugiat leo at augue. Sed ornare nibh et mi. Pellentesque erat. Pellentesque nec felis at enim vulputate consectetuer. Donec ac nibh viverra libero fermentum faucibus.';
    }

    function _print_sq($q){
        for($i = 0; $i < $q; $i++)
            print 'Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Fusce tempor. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Donec elementum augue quis orci. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos hymenaeos. Nam volutpat, nunc vel interdum bibendum, tellus felis nonummy arcu, vitae pretium quam metus quis turpis. Suspendisse risus lacus, pretium bibendum, tincidunt eget, malesuada ac, risus. Etiam ullamcorper erat et magna. Duis blandit mattis est. Maecenas suscipit, quam nec pretium convallis, nisi magna scelerisque est, sit amet lobortis justo est sed ipsum. Aenean viverra, felis ut commodo lobortis, ligula enim laoreet pede, ac tristique nisl magna quis orci. Pellentesque feugiat leo at augue. Sed ornare nibh et mi. Pellentesque erat. Pellentesque nec felis at enim vulputate consectetuer. Donec ac nibh viverra libero fermentum faucibus.';
    }

    function _echo_dq($q){
        for($i = 0; $i < $q; $i++)
            echo “Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Fusce tempor. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Donec elementum augue quis orci. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos hymenaeos. Nam volutpat, nunc vel interdum bibendum, tellus felis nonummy arcu, vitae pretium quam metus quis turpis. Suspendisse risus lacus, pretium bibendum, tincidunt eget, malesuada ac, risus. Etiam ullamcorper erat et magna. Duis blandit mattis est. Maecenas suscipit, quam nec pretium convallis, nisi magna scelerisque est, sit amet lobortis justo est sed ipsum. Aenean viverra, felis ut commodo lobortis, ligula enim laoreet pede, ac tristique nisl magna quis orci. Pellentesque feugiat leo at augue. Sed ornare nibh et mi. Pellentesque erat. Pellentesque nec felis at enim vulputate consectetuer. Donec ac nibh viverra libero fermentum faucibus.”;
    }

    function _print_dq($q){
        for($i = 0; $i < $q; $i++)
            print “Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Fusce tempor. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Donec elementum augue quis orci. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos hymenaeos. Nam volutpat, nunc vel interdum bibendum, tellus felis nonummy arcu, vitae pretium quam metus quis turpis. Suspendisse risus lacus, pretium bibendum, tincidunt eget, malesuada ac, risus. Etiam ullamcorper erat et magna. Duis blandit mattis est. Maecenas suscipit, quam nec pretium convallis, nisi magna scelerisque est, sit amet lobortis justo est sed ipsum. Aenean viverra, felis ut commodo lobortis, ligula enim laoreet pede, ac tristique nisl magna quis orci. Pellentesque feugiat leo at augue. Sed ornare nibh et mi. Pellentesque erat. Pellentesque nec felis at enim vulputate consectetuer. Donec ac nibh viverra libero fermentum faucibus.”;
    }

    function _echo_var($in, $q){
        for($i = 0; $i < $q; $i++)
            echo $in;
    }

    function _print_var($in, $q){
        for($i = 0; $i < $q; $i++)
            print $in;
    }

    function _noop(){}

    $in = 'Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Fusce tempor. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Donec elementum augue quis orci. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos hymenaeos. Nam volutpat, nunc vel interdum bibendum, tellus felis nonummy arcu, vitae pretium quam metus quis turpis. Suspendisse risus lacus, pretium bibendum, tincidunt eget, malesuada ac, risus. Etiam ullamcorper erat et magna. Duis blandit mattis est. Maecenas suscipit, quam nec pretium convallis, nisi magna scelerisque est, sit amet lobortis justo est sed ipsum. Aenean viverra, felis ut commodo lobortis, ligula enim laoreet pede, ac tristique nisl magna quis orci. Pellentesque feugiat leo at augue. Sed ornare nibh et mi. Pellentesque erat. Pellentesque nec felis at enim vulputate consectetuer. Donec ac nibh viverra libero fermentum faucibus.';
    $iter = 1;
    
    for($q = 1; $q <= 16; $q++){
        $iter = $iter * 2;
        ob_start('_noop', 65536);

        _echo_sq($iter);
        _print_sq($iter);

        _echo_dq($iter);
        _print_dq($iter);

        _echo_var($in, $iter);
        _print_var($in, $iter);

        ob_end_clean();
    }
?>

This entry was posted on Thursday, October 19th, 2006 at 11:10 pm and is tagged with . You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback.

4 Responses to “PHP Performance: Echo & Print”

  1. pitt says:

    i use this style for mulitplefile
    echo<<<phpoutput
    phpoutput;
    it slow?

  2. [...] ok so i got irritated with everyone on the intarwebs saying a bunch of **** without verifying it with facts or with WAAAAAAYYYYY too many facts like elliot jones junior over there. so i made my own test. print vs echo, here goes: [...]

  3. [...] Dan,  echo lebih cepat 20% dari print [...]

  4. Fred says:

    Thanks for that, that was quite insightful. As a PHP programmer myself however, I do firmly believe that there is no possible web application that can have a “load problem” or whatever from using print instead of echo.

    While I prefer echo, it really doesn’t matter. PHP is such a heavy language in a lot of places that it is much easier to screw up for example memory usage than from using the wrong output command…

Leave a Reply