Page 1 of 1

echo, printf or print

Posted: Thu Nov 21, 2002 8:21 am
by caseymanus
Comming from a C++ world, I tend to use printf alot more than plain print or echo,....are there any situations in which you guys think its better to use echo or print? Is there any performance gain or hit?

Posted: Thu Nov 21, 2002 8:31 am
by Johnm
Here is a previous thread concerning this.
I personally use echo though.

John M


http://forums.devnetwork.net/viewtopic.php?t=2960

Posted: Thu Nov 21, 2002 8:32 am
by twigletmac
Handily there was a bit about this in an article on zend.com:
http://www.zend.com/zend/art/mistake.php#Heading4

Basically, use printf() when you need to format the output before displaying it - probably of most use when outputting numbers. Use print() or echo() the rest of the time 'cause it's faster. echo() tends to be used more than print() not because there is any real difference between them but more because echo() has one less character to type :) .

Mac

Posted: Thu Nov 21, 2002 9:47 am
by BDKR
A couple of things that Twig didn't mention, much to my suprise as she usually covers everything; print() will return true or false as opposed to echo() wich doesn't. This may be desireable to you. Another thing to keep in mind. If you are dumping large bits of HTML to the screen....

Code: Select all

<?php
# Some code here
/* --- code --- */

# Now exit php mode
?>
<html>
    <head>
    <title>(Shameless Plug) Maximum Galoots Atypical PHP Soap Box</title>
    </head>
</html>
<?php
is faster than

Code: Select all

echo <html>;
echo     <head>;
echo     <title>(Shameless Plug) Maximum Galoots Atypical PHP Soap Box</title>;
echo     </head>;
echo </html>;
!

And uhh...., Jason. There seems to be a problem of some sort. Why is it that in the first example above, the php close tag doesn't show up?

Cheers,
BDKR