tags:

views:

256

answers:

5

I understand that echo is slightly faster, and print can be used as a function, but I've been reading an e-book on PHP, and the writer is using print, instead of echo, to output very simple text.

print "Your name is $name\n";

So my question is, when would it be appropriate for me to use print as opposed to echo?

+5  A: 

Never.

Definitely a micro optimisation.

Some may find it useful as the and print trick. But ugly as hell and not recommended.

alex
Can you elaborate a bit please?
Rob
Ah I see. Looks easier than an isset, but you're right, its ugly as hell.
Rob
However, aside from the ugliness, why don't you recommend it? Is there some drawback to it?
Rob
@Rob I was just testing it and kept getting an error. I might remove that section.
alex
Oh. So then mostly just micro optimization?
Rob
Yes, plus I'll guarantee that 99% of PHP apps out there use `echo`.
alex
Alright, thanks alex :)
Rob
About the "Unless you require to know if the print was successful or not" part - from http://www.php.net/print - "Returns 1, always."
binaryLV
@binaryLV Thanks mate, have amended.
alex
A: 

It really doesn't matter. This kind of optimization is most of the time micro optimization, which most of the time is fruitless.

Ikke
not "most of time" but just forever.
Col. Shrapnel
Well, there are maybe some obscure edgecases where it would make a difference, but 'most of the time' it does not.
Ikke
Can you name one?
Col. Shrapnel
a loop with 1e15 times echo or print would make a difference.
Ikke
+1  A: 

See this link

MAS1
So apparently print ISN'T a function after all.
Rob
@Rob I think it's a language construct, like `echo`.
alex
@alex: Yeah, apparently so. Hm. This e-book I'm reading noted echo was a language construct, but that print was a function. Unless I misread.
Rob
@Rob PHP books get things wrong all the time
alex
+1  A: 

print and echo are commands used to output information to the visitors screen (on the web page). Both do the same job, so it usually comes down to a matter of personal preference on which one you like to use.

There is a slight difference between print and echo which would depend on how you want to use the outcome. Using the print method can return a true/false value. This may be helpful during a script execution of somesort. Echo does not return a value, but has been considered as a faster executed command. All this can get into a rather complicated discussion, so for now, you can just use whichever one you prefer.

Hanseh
You *can't* get true/false with print. From manual - "Returns 1, always" (it's not even a boolean).
binaryLV
A: 

IMHO the main difference is that you can print multiple values with echo without concatenating them, i.e., echo $a, $b, $c;. As far as I know, it's not possible to do this with print. If you want to use this syntax (and I would advise to use it whenever possible, although I'm not 100% sure that it is faster in real-world apps), it would be better to always use echo, as mixing both ways would lead to inconsistency.

binaryLV