SitePoint Sponsor

User Tag List

Page 1 of 2 12 LastLast
Results 1 to 25 of 29

Thread: echo vs. print

  1. #1
    SitePoint Zealot poLka's Avatar
    Join Date
    Apr 2003
    Location
    GF
    Posts
    136
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    echo vs. print

    Heya! Could you explain to me the big differences between [print()] and [echo]. And when you'd want to use one over the other. Cheers!

  2. #2
    Mlle. Ledoyen silver trophy seanf's Avatar
    Join Date
    Jan 2001
    Location
    UK
    Posts
    7,168
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Print returns 1 on success and 0 on failure. If you don't need to check if something printed successfully then you are wasting one byte per print. So, always use echo unless you need to check that something actually printed, though I've never encountered a situation when I had to check!

    Sean
    Harry Potter

    -- You lived inside my world so softly
    -- Protected only by the kindness of your nature

  3. #3
    SitePoint Zealot poLka's Avatar
    Join Date
    Apr 2003
    Location
    GF
    Posts
    136
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hehe, thanks agian duder. cheers!

  4. #4
    Mlle. Ledoyen silver trophy seanf's Avatar
    Join Date
    Jan 2001
    Location
    UK
    Posts
    7,168
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Your welcome, again! Remember, you can always give someone positive reputation if they've helped you out - see the little green plus with an orange minus under it next to the edit button

    Sean
    Harry Potter

    -- You lived inside my world so softly
    -- Protected only by the kindness of your nature

  5. #5
    SitePoint Zealot
    Join Date
    Dec 2001
    Location
    UK
    Posts
    105
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Also, you can specify multiple, comma-delimited, arguments with echo. E.g.
    PHP Code:
    <?php
    $action 
    "Test";
    echo 
    $action" this! ""-End-";
    ?>
    So echo should win everytime. Having said that, I pretty much use print all the time [img]images/smilies/smile.gif[/img]

  6. #6
    ********* Genius Mike's Avatar
    Join Date
    Apr 2001
    Location
    Canada
    Posts
    5,458
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by seanf
    You're welcome, again! Remember, you can always give someone positive reputation if they've helped you out - see the little green plus with an orange minus under it next to the edit button

    Sean

    Mike
    It's not who I am underneath, but what I do that defines me.

  7. #7
    Mlle. Ledoyen silver trophy seanf's Avatar
    Join Date
    Jan 2001
    Location
    UK
    Posts
    7,168
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    My English ain't so good

    Sean
    Harry Potter

    -- You lived inside my world so softly
    -- Protected only by the kindness of your nature

  8. #8
    SitePoint Member
    Join Date
    May 2003
    Location
    On the Internet
    Posts
    16
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I didn't know there was a difference between those, now I know.

    Create your own e-Book...
    http://www.v-book.com Free trial available!!!

  9. #9
    SitePoint Zealot poLka's Avatar
    Join Date
    Apr 2003
    Location
    GF
    Posts
    136
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    torrent, i'm confused mate... could you clarify your last statement.

  10. #10
    ********* Wizard silver trophy Cam's Avatar
    Join Date
    Aug 2002
    Location
    Burpengary, Australia
    Posts
    4,495
    Mentioned
    0 Post(s)
    Tagged
    1 Thread(s)
    Here's a good thread on this topic

    http://www.sitepointforums.com/showt...threadid=91258

    He mean that you can pass multiple arguements to echo(), but you can't with print().

    PHP Code:
    $var1 "testing ";
    $var2 "echo() ";
    $var3 "now.";

    echo(
    $var1$var2$var3); // output: testing echo() now. 
    You can't do that with print().

  11. #11
    WebAmoeba mythix's Avatar
    Join Date
    Aug 2002
    Location
    here
    Posts
    578
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hmmmmm, so what's printf exactly?
    Laws are like sausages. You have much more respect for them if you haven't actually seen how they're made.

    http://www.webamoeba.co.uk

  12. #12
    SitePoint Zealot
    Join Date
    Dec 2001
    Location
    UK
    Posts
    105
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    <?php
    //very basic example
    $foo "One";
    $bar "Two";
    printf("Foo = %s, Bar = %s"$foo$bar);
    ?>
    Will print
    Code:
    Foo = One, Bar = Two
    You can also format the output (for example, floating point numbers, etc). More info at ye olde:

    http://www.php.net/manual/en/function.printf.php

  13. #13
    WebAmoeba mythix's Avatar
    Join Date
    Aug 2002
    Location
    here
    Posts
    578
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    More info at ye olde
    Yeah guess I could have bothered to look, lol. Is there actually any advantage of being able to do that?
    Laws are like sausages. You have much more respect for them if you haven't actually seen how they're made.

    http://www.webamoeba.co.uk

  14. #14
    SitePoint Zealot
    Join Date
    Dec 2001
    Location
    UK
    Posts
    105
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    A couple, the formatting of numbers is very useful. You might have a number like this 1.234556, but you can use printf() to display it like this 1.23, saves you having to write any code to truncate the number.

    Sometimes it's more legible if you have a number of variables within a string, to show the string as:
    PHP Code:
    printf("Error in your MySQL: Error Number [%s], Error Text [%s]\n Logged at [%s]"$errNo$errTxt$timestamp); 
    Very subjective really. Some (like me) like them, and some do not.
    If you wish to store the resultant string to a scalar then use sprintf()

  15. #15
    ********* Member website's Avatar
    Join Date
    Oct 2002
    Location
    Iceland
    Posts
    1,238
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by seanf
    Your welcome, again! Remember, you can always give someone positive reputation if they've helped you out - see the little green plus with an orange minus under it next to the edit button

    Sean
    hehe , why don't you make Important topic here that says this? I hardly get any
    - website

  16. #16
    SitePoint Wizard silver trophy redemption's Avatar
    Join Date
    Sep 2001
    Location
    Singapore
    Posts
    5,269
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by seanf
    Remember, you can always give someone positive reputation if they've helped you out - see the little green plus with an orange minus under it next to the edit button
    Nice suggestion, sean

  17. #17
    SitePoint Zealot poLka's Avatar
    Join Date
    Apr 2003
    Location
    GF
    Posts
    136
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    one more time

    Quote Originally Posted by torrent
    Also, you can specify multiple, comma-delimited, arguments with echo. E.g.
    PHP Code:
    <?php
    $action 
    "Test";
    echo 
    $action" this! ""-End-";
    ?>
    So echo should win everytime. Having said that, I pretty much use print all the time [img]images/smilies/smile.gif[/img]
    i'm sorry, i meant your closing statement... whaddya mean by it?

  18. #18
    SitePoint Zealot poLka's Avatar
    Join Date
    Apr 2003
    Location
    GF
    Posts
    136
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    print

    what about this? compared to echo...

    Code:
    function funkshun(){
      $tmp = "number";
      return $tmp;
    }
    
    $var1 = "hello";
    $var2 = funkshun();
    $var3 = 7;
    
    print( "$var1 ".$var2." $var3" );

  19. #19
    Mlle. Ledoyen silver trophy seanf's Avatar
    Join Date
    Jan 2001
    Location
    UK
    Posts
    7,168
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by website
    hehe , why don't you make Important topic here that says this? I hardly get any
    Good suggestion. Done

    Quote Originally Posted by redemption
    Nice suggestion, sean
    I thought so

    Sean
    Harry Potter

    -- You lived inside my world so softly
    -- Protected only by the kindness of your nature

  20. #20
    SitePoint Wizard silver trophy redemption's Avatar
    Join Date
    Sep 2001
    Location
    Singapore
    Posts
    5,269
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by seanf
    Good suggestion. Done
    Great!

    How about the other forums? Is it possible to make a sticky that spans the Programming forums?

  21. #21
    SitePoint Zealot poLka's Avatar
    Join Date
    Apr 2003
    Location
    GF
    Posts
    136
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    is everything about status nowadays? can't ya just take pride in giving good advice? :d cheers guys, this place is great!

  22. #22
    Mlle. Ledoyen silver trophy seanf's Avatar
    Join Date
    Jan 2001
    Location
    UK
    Posts
    7,168
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It's not about status, it helps promote the friendly attitude of the forums

    Sean
    Harry Potter

    -- You lived inside my world so softly
    -- Protected only by the kindness of your nature

  23. #23
    SitePoint Zealot poLka's Avatar
    Join Date
    Apr 2003
    Location
    GF
    Posts
    136
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    phuken eh, well i'm just playin... you know i gave you a positive when you asked earlier. how do they work anyway? i have to post so much before i can give a positive or negative?

  24. #24
    Mlle. Ledoyen silver trophy seanf's Avatar
    Join Date
    Jan 2001
    Location
    UK
    Posts
    7,168
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can give positive or negative, but the amount varies by how long you've been a member, how many posts you've made and your reputation level

    Sean
    Harry Potter

    -- You lived inside my world so softly
    -- Protected only by the kindness of your nature

  25. #25
    SitePoint Zealot
    Join Date
    Dec 2001
    Location
    UK
    Posts
    105
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by poLka
    i'm sorry, i meant your closing statement... whaddya mean by it?
    I really wouldn't spend too much time worrying about it. To be honest you use what you feel comfortable with. From a performance perspective I know of no differences between print and echo.



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
  •