Foreword: We have established that PHP is a web development language that can make developers a hefty sum of money. Before we can start learning the language, we need to know a few things. What exactly is PHP, where did it come from, and what exactly does learning the language do for developers?
The biggest triviality that plagues most newcomers to PHP is the fact that there are two commands that do (what appears to be) the exact same thing. Print and Echo both output data to the screen in a similar fashion- so why have two different commands for the same thing?
PHP Echo Vs Print Diagram
<?php print "Hello World! <br />"; echo "Hello World! <br />"; // The above outputs the text "Hello World!" on two separate lines. // Notice they are identical in output! print ("Hello World! <br />"); echo ("Hello World! <br />"); // The above are just the same, with parenthesis. // Notice both can act like functions, but note they actually aren't. ?>
In all actuality, Echo and Print differ based on how they are structured. Print returns a value much like a normal function would. But despite common belief, Print is not a function, as we can see by the fact that it doesn’t require parenthesis to work (Not to be confused with Printf). Print and Echo are actually both called language constructs, although this isn’t to say that we can’t make Print act like a function.
Developers need to ask the all-important question; “Why on Earth would I ever need to return a value from a string of data?” This is a good question, and the easy answer is you probably will never need to. The fact remains to some that returning a value degrades system performance- but is it enough to worry over?
By looping a large block of text multiple times, we can measure how long the two language constructs take to print out data. In an exclusive LearnPHPOnline.com test, we found that through an extended amount of iterations through a loop, Echo did indeed turn out to be the winner in speed! Whereas it took around 550ms for Print, the complete iteration took around 450ms for Echo. (For those without a calculator or a quick noggin, that’s almost 20% difference.)
Percent Of A Full Second (1,000ms)
Of course results depend on certain conditions, and the fact that we had to iterate the blocks of texts to an unimaginable amount of times to see a result shows that the difference really is marginal. It’s actually mentioned through a supporting page on the PHP.net homepage that developers should pick what suits them best- performance isn’t a real issue.
As a last note on speed, it’s recommended that developers add strings together via parameters- not through concatenation or multiple Echo calls. Instead of using new Echo commands to help organize code, separate them with commas (Make certain you aren’t using concatenation- this actually slows the process)! Calling the Echo or Print command multiple times will also degrade the performance of the script, although marginally, as seen below:
A Special Note On Concatenation Vs Parameters
<?php echo "Hello" . "World! <br />"; // Concatenation slows down the process because PHP must add strings together echo "Hello" , "<br />"; echo "World" , "<br />"; // Calling Echo multiple times still isn't as good as using Echo parameters solely echo "Hello" , "World!" , "<br />"; // Correct! In a large loop, this could save a couple of seconds in the long run! ?>
So what do we use? Echo of course! But not because of speed, and certainly not because we have anything against pseudo-functions that are disguised as language constructs. So why do most PHP developers go for echo, when the benefits are very marginal?
Easy! It sounds cool! Not to mention the fact that the word Echo has one less letter in it that Print- and that’s saving our left pointing finger from having to press the “T” key each time we want to use the language construct in question.
It’s human nature to be lazy (Or have a certain appreciation for cool-sounding words), and that’s exactly the reason why you’ll see the majority of PHP developers use Echo over Print. The speed benefit is just icing on the cake.
All jokes aside, it really makes no difference as to which command is used. We find that although most developers will use Echo, some of the older programmers use Print because it reminds them of their earlier programming years (Back when print was a command in early programming languages, where new language constructs weren’t stealing their glory).
Bottom line: It’s up to your tastes, and whether or not you like pressing the “T” key for nostalgic value.
Congratulation, nice article.
Nice one.
nice comparison
Not entirely complete, though. Print can be used as part of complex constructs, such as ($b) ? print “True” : print “False”; whereas Echo cannot. Also, if you want to use error output (@print”Test”;) you cannot use echo. Otherwise – good info.
Stephen, you can do exactly the same thing with echo but even more succinctly like this:
echo ($b ? “true” : “false”);
Pretty much what I expected. Personally though will stay with Print, coming from a perl background it’s too late in the game, and what’s worked since ’97 still works in ’09. :]
Good point with parameters as well. Big fan of commas.
Nice article. It really clarifies some things.
Interesting distinction ! Thanks !
Thanks.
Hi, i just wanna say that I’ve tested that in this code:
print “Hello World! “,”Again Hello World! “;
using the sign comma(,) in print does not work.
Yes, You are right !! There the difference is
Using “echo” you can print multiple values but the same is not possible with “Print”.
to stephen and altoona, you can use either print or echo with ternary conditions
print $x = ($x) ? true : false; echo $x = ($x) ? true : false;
though… this becomes slower than regular print’s or echo’s (without using ternary conditions). It is good if you don’t overuse it, otherwise, simply use regular if/else statements!
if ask in interview what should we will tel in one line
It’s a revelation, I’m developing PHP just 2 years ago but I’m using PRINT since then because I can swear I read PRINT was fastest than ECHO. Well, in my case it was a case of MISPRINT!
I’m trying to build an php output to html that contains two variables:
<img src=http://vkpromotions.com/samples/ height=406 width=325 alt=Sample /> but the output has carriage returns:
<img src=http://vkpromotions.com/samples/ height=406 width=325 alt=Sample />
vkpromotions.com/samples/1001_1027.jpg
and the browser can’t get the correct location of the image.
Any ideas?
The print command returns a 1 if successful, the extra return command is what makes the speed difference.
Hi. I did some PHP coding in the past and I had the same problem which to choose: PHP Echo or Print. After reading your text I think about it and decide that you are right.
Kevin:
The carriage returns are not being introduced by the print or echo functions. I had this problem the other day when I was reading in from a file (it read in the end-of-line characters as well), so I suspect you are doing something similar. To remove the characters, I used the trim function, like this:
$url = trim($url);
Actually the point made here about using commas instead of concatenation is incorrect. Here is some benchmark data: http://pastie.org/523020 Maybe you could provide some benchmark data of your own before making this sort of claim
Just found an excellent and recent thread on these topics: http://groups.google.com/group/make-the-web-faster/browse_thread/thread/ddfbe82dd80408cc
Nice article, thank u
Great info! Just what I was wondering myself. One thing I’m wondering about is adding the parenthesis to the code. Is there ever any benefit in using it with either echo or print?
Like this…
1. echo (“Some Text”);
Or…
2. echo “Some Text”;
Since neither echo nor print is a function it seems to be a waste, and yet I see coders doing it all of the time.
Thanks for insight but I’m still confused: is print a function or not? I’m new to PHP but having learned C++ and Java, I thought only functions had return values. And according to the PHP book I’m reading, print returns boolean values.
i knew which i should use now. This article really useful to me. Thanks!
now i know the difference bitween print and echo…
@bw: print is a language construct. I’m curious though… under what circumstance would print/echo return boolean false?
In addition to the difference between print and echo, I am interested in the difference between printing or echoing on the one hand and falling out of PHP code on the other. So that would be
(PHP processing here); echo “some plain text”; (more PHP processing);
versus
(PHP processing here); ?>some plain text<?php (more PHP processing);
I can't see much difference in performance, but what are other arguments for or against?
It would be better for newcomers to use single quotes in the examples.
really i am not expecting this much of explantion for diffrence between echo and print when i started to search. very very useful. Thanx
if $V=123; echo $v;
what is o/p?
@Nicolas Connault Nice one about the dot vs. comma. No need to rewrite anything.
There is a function which return myurl.com
I want to add myurl.com/d=d
But function echo result I can not get it how can I do that.?
I use print because I don’t know of an echo equivalent for print_r() which is exceedingly helpful for troubleshooting purposes. Using echo for strings but then print_r() for arrays feels awkward to me.
Thanks!
good tutorial
I don’t think this is the reason why most developers use echo.
Yes you are right but instead of (,) use (.) and it Will add the statements.
Hmm, for some reason, when I try Nicholas’s comma-vs-concat test, the commas are consistently (slightly) faster. Also I had to add ‘flush();’ at the end of the PHP line. (php 5.2.0 on SuSE 10.2)
php -d implicit_flush=off -r ‘$s=microtime(true); for($i=0;$i<100000;$i++) echo "omgwtf","bbq","\n"; echo microtime(true)-$s; flush();' | fgrep -v "omg" 0.069497108459473
php -d implicit_flush=off -r '$s=microtime(true); for($i=0;$i<100000;$i++) echo "omgwtf"."bbq"."\n"; echo microtime(true)-$s; flush();' | fgrep -v "omg" 0.07445502281189
if we use print(“hello”+5); then output will be 5 only bcoz it take only 1 argument.
But with echo we can do like this:- $a=5; echo “hello”.$a;
output will be hello5. +919479727024
@Nicolas Connault I took the code from http://pastie.org/523020 and I get much different results than they do. 0.043325901031494 for period concatenation and 0.034106969833374 with commas. Not sure where they get the 1.071463108062710 for commas. Using their test, I found commas to be faster.
@Juan Giordana
why use single quotes – they don’t give the same result :
$foo = “bar”;
echo ‘foo is $foo’; -> will output : foo is $foo
echo “foo is $foo”; -> will output : foo is bar
echo ‘foo is $foo \n’; echo ‘foo’; -> will output : foo is $foo \nfoo
echo “foo is $foo \n”; echo “foo”; -> will output : foo is bar foo
Print_r and print are different print is for strings and print_r is for arrays.
@Jakob, bw, bundyxc, others: echo returns nothing, print always returns 1, even if not successful. Although it could be that in the rare cases where print is unsuccessful, it couldn’t print OR return anything.
http://fi2.php.net/manual/en/function.echo.php http://fi2.php.net/manual/en/function.print.php
Echo really does sound nicer, so it’s good to know there are other reasons to use it besides that ha.
Thanks, this article cleared all my doubts about echo and print.
i have a small query here. suppose i have a php variable $empno and i want to assign it to a textbox in the form using javascript. how do i do it using php?
echo “”; echo “document.form1.txt1.value=”; —-> how would i assing $empno here? echo “”;
The echo() function is slightly faster than print(). so this is wrong in diagram.
LEGO MY ECHO
very well written
i have a small query for echo
echo “hello world”; print_r #name;
but its nice written.
@bunny -
using echo will write the output to the screen, you wouldn’t use it to generate javascript code, unless you want to write script to the screen for some reason. If that’s the case, make sure you put it in tags.
And why would you want to assign that php variable into your javascript? You’re better off just plugging that variable directly into your html form field:
<input type="text" name="txt1" value="” />
echo and print have difference.
echo does not return a value but print return 1.
try these statements separately and see the difference.
print (echo “Hello World”);
echo (print “Hello World”);
echo vs print.
The main differences between the two is that ECHO is a construct, as such it cannot be used in an expression whereas print can.
Also, you cannot use echo if your executing your php script via CLI.
this is because print returns a int value and echo returns no value / a void value.
otherwise there is no difference between print and echo. if your splitting hairs considering scipt parsing speed then you should stick to echo.
There is one case where echo cannot do what print can:
$Lord = ‘Jesus’; isset( $Lord ) and print $Lord;
@already_loving_php
That’s because concatenation is wrong. Echo uses commas and print uses a period.
Example:
<?php $name = "PHP_Noob"; $Name = "already_loving_php";
print "Your name is not $Name"; echo "” , “Your name is ${name} ” , “and not ${Name}”; print “”.”Hello http://www.LEARNPHPONLINE.com!”.”I just began learning PHP.”; ?>
Both practically does the same thing.
Right Print function is much better and faster from echo…:)