Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    New Coder
    Join Date
    Jun 2014
    Location
    London
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation (Newbie question) Echo vs print, what’s the difference?

    Okay so I am new to PHP and I was wondering what is the actual difference between the echo and print statement.

    I know the following:

    echo - can output one or more strings
    print - can only output one string, and returns always 1

    I guess what I am trying to wrap my head around is the following does it mean?

    "Print returns always 1"

    That is what is confusing me

    Thank you in advance

  • #2
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,606
    Thanks
    24
    Thanked 647 Times in 646 Posts
    It makes no difference which one you use. I have never needed to check if I wrote to the screen ["Print returns always 1"] and I really don't think you gain that much time using echo over print. I prefer to use echo. Other love print. Your choice.
    Now printf() is a whole nother story.

    By the way print can do multiple lines and work with heredoc.
    Evolution - The non-random survival of random variants.

    "If you leave hydrogen alone, for long enough, it begins to think about itself."

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,206
    Thanks
    80
    Thanked 4,566 Times in 4,530 Posts
    Sunfighter: For the "always returns 1", Den was referring to the PHP docs, which on this page

    say
    Return Values:
    Returns 1, always.
    Den: That just means you can ignore the return value from the function. In other words if you coded something like
    Code:
    if ( print("hello" ) == 1 ) { print(" world"); }
    Because print always returns 1, that code would always then also print the " world" and you'd end up with "hello world".
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,206
    Thanks
    80
    Thanked 4,566 Times in 4,530 Posts
    There is one major difference between echo and print: You are *NOT* required to use parentheses around the "argument" to echo.

    That is, you can code either
    Code:
        echo ( "example" );
    or
        echo "example";
    Whereas with print you *MUST* use the parentheses:
    Code:
        print ( "example" );
    but
        print "example"; // SYNTAX ERROR!!!!
    Finally, as a minor difference, if you *DO* use the parentheses with echo, it is *NOT* treated as a function. And there are many language constructs where trying to use it as a function will fail.

    But truthfully, it makes no sense to treat either echo or print as functions.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #5
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,132
    Thanks
    2
    Thanked 328 Times in 320 Posts
    Except that print "example"; is valid.

    print is not actually a real function (it is a language construct) so you are not required to use parentheses with its argument list.
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • #6
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,206
    Thanks
    80
    Thanked 4,566 Times in 4,530 Posts
    Huh. Wonder how I missed that when I saw it in "echo"? Sigh. Going blind as well as senile.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #7
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,441
    Thanks
    62
    Thanked 537 Times in 524 Posts
    Quote Originally Posted by CFMaBiSmAd View Post
    Except that print "example"; is valid.
    Glad I'm not the only one thinking that lol.. I was just starting to wonder how most of my code was working

    @Old Pendant... you're using double quotes there for hard coded strings in them there examples - singles would save cpu cycles
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • #8
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,206
    Thanks
    80
    Thanked 4,566 Times in 4,530 Posts
    Quote Originally Posted by tangoforce View Post
    @Old Pendant... you're using double quotes there for hard coded strings in them there examples - singles would save cpu cycles
    I assume you are talking about the fact that '...' strings don't need to be checked for $xxx or {...} inclusions. I'll bet that's true. But I will also bet that doing so isn't measurable on any PHP-based system. Not because the difference isn't measurable; just that because there are so many many many other inefficiencies in the PHP code that nobody will be able to find that difference.

    I mean, consider any PHP page that does SQL queries: One *TINY* slip in a SQL query will generate thousands if not millions of times more performance difference than using "hello" vs. 'hello'.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #9
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,206
    Thanks
    80
    Thanked 4,566 Times in 4,530 Posts
    Besides, if the PHP compiler has any cojones at all, I would *HOPE* that it would treat '...' and "..." the same if, at compile time, it doesn't detect any need to process inclusions. Actually, if I were writing a PHP compiler, I'd simply convert "..." into '...' using, if needed, the string append operator (period).

    E.g., I'd convert "The total is {$row["sum"] } not including tax" to
    Code:
        'The total is ' . $row["sum"] . ' not including tax'
    All at compile time, so there is no performance different at all at run time.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #10
    New Coder
    Join Date
    Jun 2014
    Location
    London
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks for explaining what "Print returns always 1 means" and also for the extra bits as well:

    if ( print("hello" ) == 1 ) { print(" world"); }

    I am very grateful



  •  

    Tags for this Thread

    Posting Permissions

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