Posted by & filed under Programming.

PHP has 3 functions that output text; echo, print, and printf. But, what’s the difference? More importantly, which one should be used in which situation?

On a pure speed measurement, echo is generally considered the fastest as it does not return a value like print or printf does. Being the skeptic that I am, I feel the need to do my own benchmarking; which can be found near the end of this article. But first, let’s look at why 3 different functions exist to perform the same task.

While these three functions all have the same outward function, they are performed in slightly different ways. Echo and print are very similar in that they simply dump whatever they are passed to output. Printf takes it one step further and allows you to format the data before output.

Echo uses the format echo "Hello, world";. This simply causes the string “Hello, world” to be sent to the browser (or whatever you are outputting to). Nothing is returned so you cannot use the structure $return = echo("Hello, world");.

Print can be used in much the same way as echo, with one difference. You have the ability to use a return value. Print returns the value 1 if successful, so $retval = print("Hello, world"); would set $retval = 1. This can be useful if you are doing something more complex than simply outputting text.

PrintF goes up a step on print and allows you to format the text prior to output. The formatting strings are described in the manual page for sprintf.

First, some background. The computer being used for this test is a 650mHz AMD Athlon with 512mb RAM running Mandrake Linux 10.0 and PHP version 4. The timing scripts are being run directly from the command line. All scripts measure the time directly before the testing loop for the start time and directly after the loop for the end time. Each loop is 100,000 iterations and repeated 10 times. These 10 times are divided by 10 to get an average.

For more information on timing scripts, please see one of my other articles.

The commands are:
echo "The quick brown fox jumped over the lazy dog.\n";
print "Thie quick brown fox jumped over the lazy dog.\n";
$var = print("The quick brown fox jumped over the lazy dog.\n");
printf("The quick brown fox jumped over the lazy dog.\n");
$var = printf("The quick brown fox jumped over the lazy dog.\n");

Test Results

Test # echo print $var=print printf $var=printf
1 5.548115969 5.548892021 5.687150955 5.98365593 5.985935926
2 5.523010969 5.538882971 5.707309961 5.97540307 5.978971004
3 5.526132822 5.546231985 5.695387125 5.974655151 5.981317043
4 5.532632113 5.564702988 5.69569993 5.967818975 5.978919983
5 5.538811922 5.554936886 5.699862003 5.97389102 5.981425047
6 5.530048847 5.549695015 5.695389032 5.97885704 5.986728907
7 5.526551962 5.546396971 5.702527046 5.973464966 5.987318993
8 5.522008181 5.565903902 5.705955029 5.968877077 5.981319904
9 5.53234601 5.555784941 5.693267822 5.974238873 5.97873497
10 5.525974035 5.547519922 5.690876007 5.976592064 5.981334925
Average 5.530563283 5.55189476 5.697342491 5.974745417 5.98220067

So, which one to use? Looking at these results, it appears that echo and print are really, really close in terms of speed. The difference per command was only 2/1,000,000 of a second. It just comes down to personal preference. I use echo because that’s what I used first. The speed drop on print appears to come when you assign a variable, at which point the command speed drops 1/100,000 of a second, which is still fairly minor.

3 Responses to “PHP: Benchmarking echo vs. print vs. printf”

  1. ST

    What about echo “constant string “.$var; and print “constant string “.$var; ??

  2. Mikko Rantalainen

    Note that printf() should be used like printf(“%s”, $potentially_untrusted_string) because first argument to printf() is interpreted and must be a trusted string. Also be aware of XSS attacks when you emit user inputted content.



  1.  Benchmarking PHP functions »

Leave a Reply

Your email address will not be published. Required fields are marked *