Monday, January 12, 2009

PHP print vs echo

This is a big topic for anyone writing PHP so I thought I would do my own test. I ran each 3 times ignoring the first run. With this simple test it would seams that The Data follows.

PHP 5.1.6 (cli) (built: Jul 16 2008 19:52:52)
Copyright (c) 1997-2006 The PHP Group
Zend Engine v2.1.0, Copyright (c) 1998-2006 Zend Technologies


Print
$i = 0;
for($i;$i<10000000;$i++)
{
print "Hello World.\n";
}
[blog@HomeWorkstation tmp]$ time ./print.php > /dev/null

real 0m5.894s
user 0m3.095s
sys 0m2.787s
[blog@HomeWorkstation tmp]$ time ./print.php > /dev/null

real 0m5.946s
user 0m2.943s
sys 0m2.996s
[blog@HomeWorkstation tmp]$ time ./print.php > /dev/null

real 0m5.838s
user 0m3.155s
sys 0m2.666s


Echo
$i = 0;
for($i;$i<10000000;$i++)
{
echo "Hello World.\n";
}

[blog@HomeWorkstation tmp]$ time ./echo.php > /dev/null

real 0m6.387s
user 0m3.150s
sys 0m3.208s
[blog@HomeWorkstation tmp]$ time ./echo.php > /dev/null

real 0m6.038s
user 0m3.091s
sys 0m2.928s
[blog@HomeWorkstation tmp]$ time ./echo.php > /dev/null

real 0m6.466s
user 0m3.330s
sys 0m3.110s

There are many pages out there at talk about this topic so do your own search or write your own code and let me know the results you find and I'll post them.

Here's another person's opinion.

http://www.faqts.com/knowledge_base/view.phtml/aid/1/fid/40