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
  40. k2a
    September 3, 2010

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

    Leave a reply
  41. me_is_me
    September 10, 2010

    @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

    Leave a reply
  42. Josh
    October 15, 2010

    Print_r and print are different print is for strings and print_r is for arrays.

    Leave a reply
  43. Sami
    December 9, 2010

    @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

    Leave a reply
  44. Clare
    December 30, 2010

    Echo really does sound nicer, so it’s good to know there are other reasons to use it besides that ha.

    Leave a reply
  45. Pummy Manku
    January 10, 2011

    Thanks, this article cleared all my doubts about echo and print.

    Leave a reply
  46. bunny
    January 14, 2011

    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 “”;

    Leave a reply
  47. mohsinali&fazlur
    January 21, 2011

    The echo() function is slightly faster than print().
    so this is wrong in diagram.

    Leave a reply
  48. 28
    February 6, 2011

    LEGO MY ECHO

    Leave a reply
  49. Irfan Suleman
    March 26, 2011

    very well written

    Leave a reply
  50. rajender
    April 20, 2011

    i have a small query for echo

    echo “hello world”;
    print_r #name;

    but its nice written.

    Leave a reply
  51. Gaje
    May 14, 2011

    @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="” />

    Leave a reply
  52. Ravi Shankar
    June 1, 2011

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

    Leave a reply
  53. chirag nandaniya
    July 4, 2011

    echo vs print.

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

    Leave a reply
  54. activeseven
    August 21, 2011

    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.

    Leave a reply
  55. steve
    September 26, 2011

    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.

    Leave a reply
  56. kanna
    September 27, 2011

    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.

    Leave a reply
  57. Brian Temecula
    October 2, 2011

    There is one case where echo cannot do what print can:

    $Lord = ‘Jesus’;
    isset( $Lord ) and print $Lord;

    Leave a reply
  58. PHP_Noob
    November 5, 2011

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

    Leave a reply
  59. Anil Tiwari
    November 25, 2011

    Right Print function is much better and faster from echo…:)

    Leave a reply
  60. ajay
    December 21, 2011

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

    Leave a reply
  61. Muhammad Arslan
    February 1, 2012

    Thanks, saved a lot of time… :)

    Leave a reply
  62. darklighter
    February 7, 2012

    i use print when i want to supply hard data like a form and echo when i want to insert a return like a hidden text field holding an ip address. i have worked with vbasic and other older languages so print is in my nature, although i am learning the value of using print_r to handle strings as well as arrays. so of course, instead of saving my finger from a T, i use a few MORE letters :)

    Leave a reply
  63. Devin Walker
    March 8, 2012

    I was curious about Print VS Echo in PHP and you just answered my question.

    Leave a reply
  64. mozi
    March 16, 2012

    @ajay You are right,, BTW thanks for the good article :D

    Leave a reply
  65. mediablinkk.com
    April 15, 2012

    This article misses one important thing. Echo is a language construct, that internally calls echo itself for each parameter it is passed.

    So in essence, whether you call one echo with multiple parameters using a comma or call multiple echos with one parameter each. It is exactly the same thing. Since that’s what the language construct does behind the scenes with each parameter passed to echo.

    In looking at the actual machine code called on the server when an echo using commas is called, while it’s def faster using a period/concatenation since no character by character evaluation is need. It’s actually the same if you use commas or use one echo statement for each piece of data.

    This means if you are echoing each piece of data out separately for readability and understanding, don’t worry, it’s the same as using a comma.

    Who would have thunk it? Separate echos, faster than uisng a period and one echo. =) How cool is that? Readability and performance! =)

    Leave a reply
  66. mediablinkk.com
    April 15, 2012

    oops…typing fast late at night. =)

    Meant… “In looking at the actual machine code called on the server when an echo using commas is called, while it’s def faster than using a period/concatenation since no character by character evaluation is needed. It’s actually the same if you use commas or use one echo statement for each piece of data.”

    carry on…

    Leave a reply
  67. daniel
    June 9, 2012

    With PRINT a user can INSERT A VALUE to a string. With Echo you can not do so.

    /*
    print(“”);
    print(“Input Value: “);
    print(“”);
    /*

    Leave a reply
  68. addy
    July 13, 2012

    @Nicolas Connault, I have tested your code and a different one (1M iteration) and received consistent results that are very different from the ones you’re claiming.

    comma: 04.95 | 05.31 | 05.25 | 06.04
    dot::: 12.34 | 12.63 | 12.23 | 13.06

    Leave a reply
  69. Ian
    August 8, 2012

    I’m reading a new wordpress book where the developer uses print and it always reminds me of the old “Enter” magazine programs where I would type a series of print * to make a Christmas tree or something. Although I’ve heard of the print command I never see it used so my first reaction is “this guy is old”. lol joking.

    Seriously though, interesting arguments for and against here in the comments but I think I’m sticking with echo. And as for concatenation I’ll use what ever is appropriate for the situation.
    $name = ‘fred’;
    echo ‘hello ‘ . $name;
    or
    echo “hello $name”;
    or
    echo “hello {$name};
    make more sense to me for what I’m doing and for readability than
    echo ‘hello’, $name;

    The last one makes more sense for me to echo a series of unrelated items.

    Leave a reply
  70. Al
    October 4, 2012

    @Kanna

    Print does NOT return an INT value, it returns a Boolean value

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