Jump to content

[SOLVED] Echo vs Print in PHP


per1os

Recommended Posts

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 =)

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 :)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.