PHP Echo Vs Print

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?

PHP Echo Vs Print: Which Should Developers Use?

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.

PHP Echo Vs Print: Which Is Faster?

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!
?>

PHP Echo Vs Print: The Conclusion

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.

Closing Comments

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.

Comments
  1. Niki
    April 15, 2009

    Congratulation, nice article.

    Leave a reply
  2. Rajesh
    April 21, 2009

    Nice one.

    Leave a reply
  3. Merlin
    April 24, 2009

    nice comparison

    Leave a reply
  4. stephen
    April 30, 2009

    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.

    Leave a reply
  5. altoona
    April 30, 2009

    Stephen, you can do exactly the same thing with echo but even more succinctly like this:

    echo ($b ? “true” : “false”);

    Leave a reply
  6. Mullanaphy!
    May 12, 2009

    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.

    Leave a reply
  7. Roger
    May 15, 2009

    Nice article. It really clarifies some things.

    Leave a reply
  8. PHP Expert
    May 26, 2009

    Interesting distinction !
    Thanks !

    Leave a reply
  9. gun5
    May 30, 2009

    Thanks.

    Leave a reply
  10. already_loving_php
    June 3, 2009

    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.

    Leave a reply
  11. Rama akella
    June 24, 2009

    Yes, You are right !! There the difference is

    Using “echo” you can print multiple values but the same is not possible with “Print”.

    Leave a reply
  12. pedro
    July 6, 2009

    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!

    Leave a reply
  13. bharathi
    July 12, 2009

    if ask in interview what should we will tel in one line

    Leave a reply
  14. Oscar
    July 24, 2009

    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!

    Leave a reply
  15. Kevin
    July 28, 2009

    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:

    vkpromotions.com/samples/1001_1027.jpg

    and the browser can’t get the correct location of the image.

    Any ideas?

    Leave a reply
  16. Jakob
    July 28, 2009

    The print command returns a 1 if successful, the extra return command is what makes the speed difference.

    Leave a reply
  17. beli
    July 29, 2009

    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.

    Leave a reply
  18. Jonathan
    August 12, 2009

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

    Leave a reply
  19. Nicolas Connault
    August 27, 2009

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

    Leave a reply
  20. Nicolas Connault
    August 27, 2009

    Just found an excellent and recent thread on these topics: http://groups.google.com/group/make-the-web-faster/browse_thread/thread/ddfbe82dd80408cc

    Leave a reply
  21. janis
    September 14, 2009

    Nice article, thank u :)

    Leave a reply
  22. Grand Poobah
    October 10, 2009

    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.

    Leave a reply
  23. bw
    October 16, 2009

    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.

    Leave a reply
  24. Vo Thanh Liem
    October 24, 2009

    i knew which i should use now. This article really useful to me. Thanks!

    Leave a reply
  25. Anthoni Raj
    November 22, 2009

    now i know the difference bitween print and echo…

    Leave a reply
  26. bundyxc
    December 21, 2009

    @bw: print is a language construct. I’m curious though… under what circumstance would print/echo return boolean false?

    Leave a reply
  27. Pim
    January 5, 2010

    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?

    Leave a reply
  28. Juan Giordana
    January 21, 2010

    It would be better for newcomers to use single quotes in the examples.

    Leave a reply
  29. Mohamed Ismail
    February 6, 2010

    really i am not expecting this much of explantion for diffrence between echo and print when i started to search. very very useful. Thanx

    Leave a reply
  30. abhijeet jadhav
    March 1, 2010

    if
    $V=123;
    echo $v;

    what is o/p?

    Leave a reply
  31. sims
    March 10, 2010

    @Nicolas Connault
    Nice one about the dot vs. comma. No need to rewrite anything.

    Leave a reply
  32. Satılık Yat
    April 5, 2010

    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.?

    Leave a reply
  33. BMFNC
    April 9, 2010

    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.

    Leave a reply
  34. Nelson
    April 13, 2010

    Thanks! :D

    Leave a reply
  35. vasanth
    April 27, 2010

    good tutorial

    Leave a reply
  36. Narendra
    May 16, 2010

    I don’t think this is the reason why most developers use echo.

    Leave a reply
  37. Ajit
    June 17, 2010

    Yes you are right but instead of (,) use (.) and it Will add the statements.

    Leave a reply
  38. Tom Hundt
    June 29, 2010

    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

    Leave a reply
  39. prtk
    July 4, 2010

    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

    Leave a reply
Leave a Comment Below »
Your Name
Your Email Address
Your Comment