SitePoint Sponsor

User Tag List

Results 1 to 14 of 14
  1. #1
    SitePoint Evangelist
    Join Date
    Nov 2001
    Location
    UK
    Posts
    553
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    echo() or print() ???

    There are, as I'm sure you know, different ways to echo data from a database onto a HTML page. I know of echo() and print().

    Which do you use, and why? Which is better?
    Regards, Ant.

  2. #2
    What? Maelstrom's Avatar
    Join Date
    Oct 2001
    Location
    Whistler BC originally from Guelph Ontario
    Posts
    2,175
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The main difference between the two are what they return. I believe print returns a boolean variable allowing you to test if it worked. Echo only does the work. Otherwise they are identical.

    Also do a search in the forums this is a common question
    Maelstrom Personal - Apparition Visions
    Development - PhP || Mysql || Zend || Devshed
    Unix - FreeBSD || FreeBsdForums || Man Pages
    They made me a sitepoint Mentor - Feel free to PM me or Email me and I will see if I can help.

  3. #3
    SitePoint Wizard Defender1's Avatar
    Join Date
    Apr 2001
    Location
    My Computer
    Posts
    2,808
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i use echo.
    though i don't use the parens.
    just
    PHP Code:
    echo "Hi folks!"
    Though let it be known now, echo is a language construct, not a function.
    Defender's Designs
    I'm Getting Married!

    Not-so-patiently awaiting Harry Potter Book 7 *sigh*

  4. #4
    SitePoint Addict eddiembabaali's Avatar
    Join Date
    Mar 2001
    Location
    USA, Philadelphia
    Posts
    206
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I also use echo more often than print

    PHP Code:
    echo "whaaaaaaa"  " Zaaaaaaaaaaaaaaa"
    cd pub \r; more beer

    Eddie

  5. #5
    SitePoint Wizard Defender1's Avatar
    Join Date
    Apr 2001
    Location
    My Computer
    Posts
    2,808
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i really don't use the concantenation(sp?) all that much.
    Defender's Designs
    I'm Getting Married!

    Not-so-patiently awaiting Harry Potter Book 7 *sigh*

  6. #6
    SitePoint Wizard silver trophy redemption's Avatar
    Join Date
    Sep 2001
    Location
    Singapore
    Posts
    5,269
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by Defender1
    i really don't use the concantenation(sp?) all that much.
    it's concatenation defender1

    and if you can still remember me saying i prefer to think of echo as a function, thereby adding the braces ()... well, hehe i take that back...

    i said that when i'd never done any actual coding so i had no idea... now that i'm actually typing in, i prefer to leave out the braces... i goofed didn't i lol

  7. #7
    Making a better wheel silver trophy DR_LaRRY_PEpPeR's Avatar
    Join Date
    Jul 2001
    Location
    Missouri
    Posts
    3,428
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by eddiembabaali
    I also use echo more often than print

    PHP Code:
    echo "whaaaaaaa" " Zaaaaaaaaaaaaaaa"
    use a comma rather than concatenation:

    PHP Code:
    echo "whaaaaaaa"" Zaaaaaaaaaaaaaaa"
    more effecient = good.
    - Matt ** Ignore old signature for now... **
    Dr.BB - Highly optimized to be 2-3x faster than the "Big 3."
    "Do not enclose numeric values in quotes -- that is very non-standard and will only work on MySQL." - MattR

  8. #8
    What? Maelstrom's Avatar
    Join Date
    Oct 2001
    Location
    Whistler BC originally from Guelph Ontario
    Posts
    2,175
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by DR_LaRRY_PEpPeR


    use a comma rather than concatenation:

    PHP Code:
    echo "whaaaaaaa"" Zaaaaaaaaaaaaaaa"
    more effecient = good.
    Thats the first time I have ever seen using a comma.
    Maelstrom Personal - Apparition Visions
    Development - PhP || Mysql || Zend || Devshed
    Unix - FreeBSD || FreeBsdForums || Man Pages
    They made me a sitepoint Mentor - Feel free to PM me or Email me and I will see if I can help.

  9. #9
    SitePoint Addict AbelaJohnB's Avatar
    Join Date
    Oct 2001
    Location
    Eureka, CA. USA
    Posts
    250
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by DR_LaRRY_PEpPeR


    use a comma rather than concatenation:

    PHP Code:
    echo "whaaaaaaa"" Zaaaaaaaaaaaaaaa"
    more effecient = good.

    more effecient

    Umm, "more effecient" based upon what??


    Sorry, but I do not see where "," would be 'more effecient' than '.' so please explain yourself.



    Later
    John B. Abela
    www.JohnAbela.Com

  10. #10
    SitePoint Wizard silver trophy redemption's Avatar
    Join Date
    Sep 2001
    Location
    Singapore
    Posts
    5,269
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by DR_LaRRY_PEpPeR


    use a comma rather than concatenation:

    more effecient = good.
    wow i didn't know that... and i also want to know why

  11. #11
    Happy Holidays !! Paul S's Avatar
    Join Date
    Mar 2001
    Location
    Mexico
    Posts
    1,287
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by Maelstrom


    Thats the first time I have ever seen using a comma.
    Actually it's in the manual, I used to use the dot too but not anymore

    And yes, as DR_LaRRY_PEpPeR said if you use the comma it's more efficient, why? Well because PHP only has to print out two separate strings, on the other hand, with the dot it has to concatenate them first (I'm guessing using a temporal variable or something, ie, it wastes memory) and then print them out.

  12. #12
    Making a better wheel silver trophy DR_LaRRY_PEpPeR's Avatar
    Join Date
    Jul 2001
    Location
    Missouri
    Posts
    3,428
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by AbelaJohnB
    Umm, "more effecient" based upon what??


    Sorry, but I do not see where "," would be 'more effecient' than '.' so please explain yourself.
    theory. the difference has been explained many times in this forum (mostly by Skunk, i think).

    "." concatenates the the string on both sides, and returns it, as you know. so, when you use "." the total string is getting concatenated, returned, and echoed. with "," each string just gets echoed, not put together and returned. so, it's like saying:

    echo $txt; echo $txt; echo $txt;

    i had never benchmarked the 2 methods, but i just did, and there IS basically NO difference. although, using "," definitely isn't slower, so i will continue to use it. and the way i code, "," saves me 1 byte of filesize (since i don't put a space on each side, as with "."). e.g. "echo $txt, $txt" instead of "echo $txt . $txt"

    here's the code i tested with:

    PHP Code:
    <?php

    $txt 
    'This is some text! This is some text! This is some text! This is some text!
        This is some text! This is some text! This is some text! This is some text!
        This is some text! This is some text! This is some text! This is some text!
        This is some text! This is some text! This is some text! This is some text!
        This is some text! This is some text! This is some text! This is some text!'
    ;

    ob_start();

    $mt explode(' 'microtime());
    $start $mt[0] + $mt[1];

    for (
    $i 0$i 1000$i++) { echo $txt$txt$txt$txt$txt$txt; }

    $mt explode(' 'microtime());
    $end $mt[0] + $mt[1];

    ob_end_clean();

    echo 
    $end $start'<br />';

    ob_start();

    $mt explode(' 'microtime());
    $start $mt[0] + $mt[1];

    for (
    $i 0$i 1000$i++) { echo $txt$txt$txt$txt$txt$txt; }

    $mt explode(' 'microtime());
    $end $mt[0] + $mt[1];

    ob_end_clean();

    echo 
    $end $start;

    ?>
    EDIT: i see Paul already explained. yeah, i'd say it's more memory effecient, instead of speed effecient, depending how long your strings are, of course. but, then, text prob doesn't take much memory anyway, but i'm of the opinion "every little bit helps." so, even when something is minutely faster/more effecient, and it accomplishes the same task, i use it.
    Last edited by DR_LaRRY_PEpPeR; Dec 1, 2001 at 00:23.

  13. #13
    SitePoint Addict eddiembabaali's Avatar
    Join Date
    Mar 2001
    Location
    USA, Philadelphia
    Posts
    206
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have never ever seen the comma before .. Something new for me .. thanks for the tip Larry
    cd pub \r; more beer

    Eddie

  14. #14
    What? Maelstrom's Avatar
    Join Date
    Oct 2001
    Location
    Whistler BC originally from Guelph Ontario
    Posts
    2,175
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well thats something I will have to remember...it is also something I am surprised the manual has not mentioned. PHP manual is usually excellent at describing speed differences.
    Maelstrom Personal - Apparition Visions
    Development - PhP || Mysql || Zend || Devshed
    Unix - FreeBSD || FreeBsdForums || Man Pages
    They made me a sitepoint Mentor - Feel free to PM me or Email me and I will see if I can help.


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
  •