per1os Posted March 23, 2007 Share Posted March 23, 2007 In case anyone was wondering I decided to test which was quicker using 2 different size strings, one a small 25 character the other a big > 400 character for echo and print. Here were my results: small results echo = 1.49350118637 print = 1.50529193878 big results echo = 32.9350512028 print = 31.7385149002 As you can tell there is really not much of a difference between the two. With 50,000 lines printed for each there is not much a difference. A hundredth of a second for the small string and 1.2 seconds for 50,000 lines for the big string. Just kind of an FYI for everyone, it does not really matter which you use, either is very efficient for the amount that most people print =) Quote Link to comment https://forums.phpfreaks.com/topic/43919-solved-echo-vs-print-in-php/ Share on other sites More sharing options...
genericnumber1 Posted March 23, 2007 Share Posted March 23, 2007 ... echo and print are aliases of each other.... they're exactly the same in every single way other than letters... run that script 1000 times, average them out, and you'll see they're (relatively) the same, that is unless the server hiccups or something. Quote Link to comment https://forums.phpfreaks.com/topic/43919-solved-echo-vs-print-in-php/#findComment-213226 Share on other sites More sharing options...
emehrkay Posted March 23, 2007 Share Posted March 23, 2007 echo can print more than one string at a time echo $x, $y, $z; print cannot do that Quote Link to comment https://forums.phpfreaks.com/topic/43919-solved-echo-vs-print-in-php/#findComment-213227 Share on other sites More sharing options...
ridiculous Posted March 23, 2007 Share Posted March 23, 2007 This discussion is so hawt. I'm sticking with echo. Straight up. Quote Link to comment https://forums.phpfreaks.com/topic/43919-solved-echo-vs-print-in-php/#findComment-213236 Share on other sites More sharing options...
genericnumber1 Posted March 23, 2007 Share Posted March 23, 2007 Ah, I thought print could take multiple parameters as well. Quote Link to comment https://forums.phpfreaks.com/topic/43919-solved-echo-vs-print-in-php/#findComment-213239 Share on other sites More sharing options...
btherl Posted March 23, 2007 Share Posted March 23, 2007 I'm a print man myself.. and I think it's time we answered this question once and for all.. .. with a FIGHT TO THE DEATH! And while we're on the topic of miniscule gains in efficiency, single quotes are slightly faster than double quotes. Quote Link to comment https://forums.phpfreaks.com/topic/43919-solved-echo-vs-print-in-php/#findComment-213240 Share on other sites More sharing options...
per1os Posted March 23, 2007 Author Share Posted March 23, 2007 Print can also take multiple param IE: print $x . $b . $c; To be honest I like print better =) Quote Link to comment https://forums.phpfreaks.com/topic/43919-solved-echo-vs-print-in-php/#findComment-213248 Share on other sites More sharing options...
ridiculous Posted March 23, 2007 Share Posted March 23, 2007 Typing echo takes one less keystroke. Echo wins. But user authorization help? That loses. Big time. Quote Link to comment https://forums.phpfreaks.com/topic/43919-solved-echo-vs-print-in-php/#findComment-213252 Share on other sites More sharing options...
rajeshrhino Posted March 23, 2007 Share Posted March 23, 2007 print is a function whereas echo is a language construct...but they're basically an alias of one another for the most part. and echo has the slight performance advantage because it doesn't have a return value. however the main difference is echo() can take multiple expressions whereas print() cannot take multiple expressions. Quote Link to comment https://forums.phpfreaks.com/topic/43919-solved-echo-vs-print-in-php/#findComment-213257 Share on other sites More sharing options...
kenrbnsn Posted March 23, 2007 Share Posted March 23, 2007 print is a function whereas echo is a language construct That's not what the manual says: print() is not actually a real function (it is a language construct) so you are not required to use parentheses with its argument list. Ken Quote Link to comment https://forums.phpfreaks.com/topic/43919-solved-echo-vs-print-in-php/#findComment-213259 Share on other sites More sharing options...
per1os Posted March 23, 2007 Author Share Posted March 23, 2007 Found this: What is the difference between echo and print? Which is faster, echo or print? Mar 6th, 2006 11:50 Joshua McGinnis, geozipp, Nathan Wallace, Rasmus Lerdorf There is a difference between the two, but speed-wise it should be irrelevant which one you use. print() behaves like a function in that you can do: $ret = print "Hello World";And $ret will be 1That means that print can be used as part of a more complex expression where echo cannot. print is also part of the precedence table which it needs to be if it is to be used within a complex expression. It is just about at the bottom of the precendence list though. Only "," AND, OR and XOR are lower.echo is marginally faster since it doesn't set a return value if you really want to get down to the nitty gritty.If the grammar is: echo expression [, expression[, expression] ... ]Then echo ( expression, expression ) is not valid. ( expression ) reduces to just an expression so this would be valid: echo ("howdy"),("partner");but you would simply write this as: echo "howdy","partner"; if you wanted to use two expression. Putting the brackets in there serves no purpose since there is no operator precendence issue with a single expression like that. http://www.faqts.com/knowledge_base/view.phtml/aid/1/fid/40 That was linked to from the http://www.php.net/echo site. Basically they are the same thing which kind of proves it does not matter which. I am currently running a script that gathers the average of what I ran earlier after running it a # of times, after I get it I will post the script and the averages I received just for fun. Quote Link to comment https://forums.phpfreaks.com/topic/43919-solved-echo-vs-print-in-php/#findComment-213260 Share on other sites More sharing options...
emehrkay Posted March 23, 2007 Share Posted March 23, 2007 Print can also take multiple param IE: print $x . $b . $c; To be honest I like print better =) function disp(){ return 'foo';} echo 'this is my', disp(); can print do that? Quote Link to comment https://forums.phpfreaks.com/topic/43919-solved-echo-vs-print-in-php/#findComment-213271 Share on other sites More sharing options...
per1os Posted March 23, 2007 Author Share Posted March 23, 2007 function disp(){ return 'foo';} print 'this is my' . disp(); Seems like it. Quote Link to comment https://forums.phpfreaks.com/topic/43919-solved-echo-vs-print-in-php/#findComment-213272 Share on other sites More sharing options...
emehrkay Posted March 23, 2007 Share Posted March 23, 2007 function disp(){ return 'foo';} print 'this is my' . disp(); Seems like it. you're concatenating try this one $a = array(1,2,3,4); echo '<pre>', print_r($a), '</pre>'; there is an order defined here Quote Link to comment https://forums.phpfreaks.com/topic/43919-solved-echo-vs-print-in-php/#findComment-213273 Share on other sites More sharing options...
per1os Posted March 23, 2007 Author Share Posted March 23, 2007 Yea, I know I am concatenating. EDIT:: I understand how the above makes a difference now, thanks emehrkay! ::EDIT. Anyhow I created my script with these results For echo big it took an average of 9.81812108647 seconds! For echo small it took an average of 0.0976919044148 seconds! For print big it took an average of 0.0901416431774 seconds! For print small it took an average of 0.0952144319361 seconds! Although I am not sure about the print big, here is the script, maybe I got something wrong. This is the script: <?php set_time_limit(900); function microtime_float() { $time = microtime(); return (double)substr( $time, 11 ) + (double)substr( $time, 0, 8 ); } function bigStringEcho() { $time_start = microtime_float(); $string = "this is a big stringthis is a big stringthis is a big stringthis is a big stringthis is a big stringthis is a big stringthis is a big stringthis is a big stringthis is a big stringthis is a big stringthis is a big stringthis is a big stringthis is a big stringthis is a big stringthis is a big stringthis is a big stringthis is a big stringthis is a big stringthis is a big stringthis is a big stringthis is a big stringthis is a big stringthis is a big stringthis is a big stringthis is a big stringthis is a big stringthis is a big stringthis is a big stringthis is a big stringthis is a big stringthis is a big stringthis is a big stringthis is a big stringthis is a big string"; $i=0; while ($i < 25000) { echo $string . "<br />"; $i++; } $time_end = microtime_float(); return ($time_end - $time_start); } function bigStringPrint() { $time_start = microtime_float(); $string = "this is a big stringthis is a big stringthis is a big stringthis is a big stringthis is a big stringthis is a big stringthis is a big stringthis is a big stringthis is a big stringthis is a big stringthis is a big stringthis is a big stringthis is a big stringthis is a big stringthis is a big stringthis is a big stringthis is a big stringthis is a big stringthis is a big stringthis is a big stringthis is a big stringthis is a big stringthis is a big stringthis is a big stringthis is a big stringthis is a big stringthis is a big stringthis is a big stringthis is a big stringthis is a big stringthis is a big stringthis is a big stringthis is a big stringthis is a big string"; $i=0; while ($i < 25000) { print $string . "<br />"; $i++; } $time_end = microtime_float(); return ($time_end - $time_start); } function smallStringEcho() { $time_start = microtime_float(); $string = "this is a small string"; $i=0; while ($i < 50000) { echo $string . "<br />"; $i++; } $time_end = microtime_float(); return ($time_end - $time_start); } function smallStringPrint() { $time_start = microtime_float(); $string = "this is a small string"; $i=0; while ($i < 50000) { print $string . "<br />"; $i++; } $time_end = microtime_float(); return ($time_end - $time_start); } ob_start(); // stop output $i=0; while ($i < 10) { $ecBgTime[$i] = bigStringEcho(); ob_clean(); $ptBgTime[$i] = bigStringPrint(); ob_clean(); $ecSmTime[$i] = smallStringEcho(); $ptSmTime[$i] = smallStringPrint(); ob_clean(); $i++; } ob_end_clean(); // ok lets output again! foreach ($ecBgTime as $key => $val) { $ecAverageBg += $ecBgTime[$key]; $ecAverageSm += $ecSmTime[$key]; $ptAverageBg += $ptBgTime[$key]; $ptAverageSm += $ptSmTime[$key]; } $ecAverageBg = ($ecAverageBg / $i); $ecAverageSm = ($ecAverageSm / $i); $ptAverageBg = ($ptAverageBg / $i); $ptAverageSm = ($ptAverageSm / $i); print "For echo big it took an average of " . $ecAverageBg . " seconds!<br /> "; print "For echo small it took an average of " . $ecAverageSm . " seconds!<br /><br /> "; print "For print big it took an average of " . $ptAverageBg . " seconds!<br /> "; print "For print small it took an average of " . $ptAverageSm . " seconds!<br />"; ?> Note this is running on my Apache 2 with PHP 4.4 server on XP. Took roughly 3 minutes to run. Quote Link to comment https://forums.phpfreaks.com/topic/43919-solved-echo-vs-print-in-php/#findComment-213274 Share on other sites More sharing options...
per1os Posted March 23, 2007 Author Share Posted March 23, 2007 function disp(){ return 'foo';} print 'this is my' . disp(); Seems like it. you're concatenating try this one $a = array(1,2,3,4); echo '<pre>', print_r($a), '</pre>'; there is an order defined here Ahh, now I can see the difference man. I honestly always wondered why I could never do the print_r() function inside of a print statement. I now know why. You learn something new every day. Quote Link to comment https://forums.phpfreaks.com/topic/43919-solved-echo-vs-print-in-php/#findComment-213275 Share on other sites More sharing options...
btherl Posted March 23, 2007 Share Posted March 23, 2007 You can do $a = array(1,2,3,4); print "<pre>" . print_r($a,true) . "</pre>"; but not with var_dump() which can't return its output. So in the case where you want to display several strings including the output of var_dump() or some similar function which displays output, and you want it all in a single statement, THEN you need to use echo and not print Quote Link to comment https://forums.phpfreaks.com/topic/43919-solved-echo-vs-print-in-php/#findComment-213296 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.