SitePoint Sponsor

User Tag List

View Poll Results: which do you use?

Voters
25. You may not vote on this poll
  • echo

    20 80.00%
  • print

    5 20.00%
Results 1 to 11 of 11

Thread: echo vs print

  1. #1
    Rusted & Weathered HumanClay's Avatar
    Join Date
    Sep 2000
    Location
    Milwaukee, WI
    Posts
    225
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    echo vs print

    Which do you use and why?

    Also, are all of these valid php statements? Is one method better than the other speed wise?

    PHP Code:
    echo '<div align="center">Welcome. <a href="/">Click here to return home</a></div>';
    echo 
    "<div align=\"center\">Welcome. <a href=\"/\">Click here to return home</a></div>";
    print 
    '<div align="center">Welcome. <a href="/">Click here to return home</a></div>';
    print 
    "<div align=\"center\">Welcome. <a href=\"/\">Click here to return home</a></div>"
    Steve Caponetto - [profile] [e-mail]
    CreedFeed.com - feed your need for Creed!

  2. #2
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    echo is faster than print(), and php parses strings between double quotes. It does not parse strings between single quotes. So, if you don't have any variables in your strings, you should enclose them in single quotes to speed things up. See these links:

    http://www.faqts.com/knowledge_base/...l/aid/1/fid/40

    http://www.zend.com/zend/tut/using-strings.php

  3. #3
    <? echo "Kick me"; ?> petesmc's Avatar
    Join Date
    Nov 2000
    Location
    Hong Kong
    Posts
    1,508
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Also, you can't do this with print:

    echo $var,$var2,$var3,$var4;

  4. #4
    SitePoint Member Craig Armstrong's Avatar
    Join Date
    Jun 2001
    Location
    Vancouver, WA, USA
    Posts
    20
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    echo
    --Craig Armstrong

    My site: coldplayonline.com
    Homepage: obscured.reflections

  5. #5
    ********* Callithumpian silver trophy freakysid's Avatar
    Join Date
    Jun 2000
    Location
    Sydney, Australia
    Posts
    3,798
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    print is a function. It returns a value (true of false). echo does not return a value because it is a language construct (whatever that is ). echo is more efficient as already mentioned.

    Originally posted by petesmc
    Also, you can't do this with print:

    echo $var,$var2,$var3,$var4;
    Pete, I'm glad you brought this up. I only just realised this the other day myself (I think from rereading the zend strings article 7stud mentions). This is far more efficient that creating a concatenated string and feeding that to echo. Eg:

    echo 'var1 is : ' . $var1 . ' var2 is : ' . $var2;

    requires that a concatenated string be created (in memory) and that string be fed to echo. Whereas,

    echo 'var1 is : ', $var1 , ' var2 is : ', $var2;

    just echos each arguement without creating any additional overhead in memory.

    Those familiar with C++ can see that this is equivilent to:

    cout << "var1 is : " << var1 << "var2 is : " << var2;

  6. #6
    Rusted & Weathered HumanClay's Avatar
    Join Date
    Sep 2000
    Location
    Milwaukee, WI
    Posts
    225
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by freakysid
    echo 'var1 is : ' . $var1 . ' var2 is : ' . $var2;

    requires that a concatenated string be created (in memory) and that string be fed to echo. Whereas,

    echo 'var1 is : ', $var1 , ' var2 is : ', $var2;

    just echos each arguement without creating any additional overhead in memory.
    Wow, thanks for that! I did not know that. Question: How much of a memory saver is the , method? I assume it isn't that much because I've been doing this by concatinating strings together and so far I havent used up server memory
    Steve Caponetto - [profile] [e-mail]
    CreedFeed.com - feed your need for Creed!

  7. #7
    ********* Callithumpian silver trophy freakysid's Avatar
    Join Date
    Jun 2000
    Location
    Sydney, Australia
    Posts
    3,798
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hehe, most of the time memory is not an issue! However, technically this:

    echo 'this is foo : ', $foo;

    is more efficient than:

    echo 'this is foo : ' . $foo;

    is more efficient than:

    echo "this is foo : $foo";

    But, yeah, most of the time you are not worrying about using up memory (otherwise we would all be programming in C or assembler and compiling binaries ). However, I am writing some scripts at the moment for a web application which it is hoped will outgrow a single server (wishful thinking?). So I am trying my best to write efficient code and database queries (well as much as my limited knowledge allows).

  8. #8
    SitePoint Wizard TWTCommish's Avatar
    Join Date
    Aug 1999
    Location
    Pittsburgh, PA, USA
    Posts
    3,910
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I use printf() anything I'm outputting HTML. Every single time. I use echo and print off and on -- but printf() is what I use most of the time.

  9. #9
    Grumpy Mole Man Skunk's Avatar
    Join Date
    Jan 2001
    Location
    Lawrence, Kansas
    Posts
    2,066
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ooh I didn't even know there was a difference between print and echo - I tend to use echo for most things and I'll definitely have a go at that comma trick.

  10. #10
    SitePoint Addict zoordaan's Avatar
    Join Date
    Feb 2001
    Location
    NYC/Texas
    Posts
    348
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sid, thanks for the info on using commas. I've been using the period to concatenate all this time.

  11. #11
    midnight coder
    Join Date
    Dec 2000
    Location
    The flat edge of the world
    Posts
    838
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I just use echo nearly all the time, it does pretty much everything print and printf does, well as much as I need it to do anyway.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •