Print or Echo?

Discussion in 'PHP' started by Bram Wenting, May 5, 2007.

?

Echo or Print?

  1. Echo

    13 vote(s)
    86.7%
  2. Print

    2 vote(s)
    13.3%
  1. #1
    Hi!

    I was wondering, what could I use best for printing text in PHP: Echo or Print?
    And why should it be Echo or Print?
    I heard that servers can process Print faster, is this true?

    Thanks for sharing your opinions and knowledge!

    Bram Wenting
     
    Bram Wenting, May 5, 2007 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    echo is faster cause it doesn't leave a return value like print. But this is really nothing you should worry about.. use whatever you prefer.
     
    nico_swd, May 5, 2007 IP
  3. rodney88

    rodney88 Guest

    Messages:
    480
    Likes Received:
    37
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Afaik echo is marginally faster as it does not return a value (true/false on success/failure) but there's really not much in it.

    Also when outputting multiple variables and strings you don't need to concate into a single argument but can pass each variable as a separate parameter:
    echo $var, $var2, $var3;
    print $var.$var2.$var3
     
    rodney88, May 5, 2007 IP
  4. webmaster@newberryautomot

    webmaster@newberryautomot Peon

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    If I have to print a value(s) to a page i use print because I can usually format the line or variables that need to be printed.

    ex.
    print '<h3><center><b>' . $this->errmsg . '</h3></center></b><p><br>';

    This way I can use html or php formatting to give a cleaner effect to the screen for viewers that go to my site.
     
  5. DXL

    DXL Peon

    Messages:
    380
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    0
    #5
    I use print for an odd reason, namely that if I'd use echo I might get confused in perl ;)
     
    DXL, May 5, 2007 IP
  6. webmaster@newberryautomot

    webmaster@newberryautomot Peon

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    I would have to agree DXL plus I am use to using print from writing C++ code all the time. I only used echo when I first started programming.
     
  7. fireshark

    fireshark Peon

    Messages:
    190
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #7
    I use echo...
     
    fireshark, May 5, 2007 IP
  8. feyd

    feyd Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    You can find more information about print() and echo() functions on php.net/print.
     
    feyd, May 5, 2007 IP
  9. rays

    rays Active Member

    Messages:
    563
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    58
    #9
    There is a some difference between the two, but speed-wise it should be irrelevant which one you use. print() behaves like a function in that you can do:

    $ret = print "Hello World";

    and $ret will be 1.

    That means that print can be used as part of a more complex expression where echo cannot. print is also part of the precedence table which it needs to be if it is to be used within a complex expression. It is just about at the bottom of the precedence list though. Only "," AND, OR and XOR are lower.

    echo is marginally faster since it doesn't set a return value if you really want to get down to the nitty gritty.

    If the grammar is:

    echo expression [, expression[, expression] ... ]

    then

    echo ( expression, expression )

    is not valid. ( expression ) reduces to just an expression so this would be valid:

    echo ("howdy"),("partner");

    but you would simply write this as:

    echo "howdy","partner";

    if you wanted to use two expressions. Putting the brackets in there serves no purpose since there is no operator precedence issue with a single expression like that.

    Feel free to use any according to your needs. *NIX geeks will prefer "echo" though.
     
    rays, May 7, 2007 IP