echo, printf or print

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
caseymanus
Forum Commoner
Posts: 34
Joined: Wed Nov 20, 2002 10:32 pm
Contact:

echo, printf or print

Post 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?
User avatar
Johnm
Forum Contributor
Posts: 344
Joined: Mon May 13, 2002 12:05 pm
Location: Michigan, USA
Contact:

Post by Johnm »

Here is a previous thread concerning this.
I personally use echo though.

John M


http://forums.devnetwork.net/viewtopic.php?t=2960
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
User avatar
BDKR
DevNet Resident
Posts: 1207
Joined: Sat Jun 08, 2002 1:24 pm
Location: Florida
Contact:

Post 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
Post Reply