11

Possible Duplicate:
How are echo and print different in PHP?

UPDATE :

I found to my relief an exact duplicate (it wasn't showing up when I typed this question at first, I found it with ... google): Please vote with me to close this question, because it's tiring, go hunt that other poor guy a bit ;-)


Is there any difference between print and echo in PHP? If so, which should I use and when? If not, why are there two keywords?

UPDATE :

At the downvoters : please read the SO faq. SO was setup also to capture googleable questions. so you shouldn't downvote for that, this question is a valid question, answered on a lot of places and now on SO too.

Of course you can downvote for another reason, but please leave a comment in the lines of -1 : downvoted for .. , cause for now, I'm not understanding the downvotes.

Community
  • 1
  • 1
Peter
  • 47,963
  • 46
  • 132
  • 181
  • 7
    "Googleable" as a tag implies that you are terribly, terribly lazy for asking a question. I removed it to protect you from yourself. – Welbog Jun 17 '09 at 12:06
  • 2
    @Welbog, that's fine for me, but I put it there with some reaons. 1) using SO in stead of google is OK, its meant score high on google so it has to have coverage of existing answers. Not everyone seems to know this, normally if I ask a googleable question, I am voted down, certainly by newbies, since on ohter forums, asking googleable questions is not done, so in way it is to protect myself 2. By stating googleable, i hope on quick responses of people who will google it, so I can really use it in stead of google , while still helping extending the knowledge base here – Peter Jun 17 '09 at 12:12
  • 4
    I have to agree with Peter. That fact that something is "googleable" shouldn't mean anything. The vast majority of questions here could be considered "googleable." – Sampson Jun 17 '09 at 12:14
  • 2
    @Jonathan : Than again : since the majority of questions is googleable, the tag might not be that usefull :-) – Peter Jun 17 '09 at 12:18
  • I wonder who invented "RTFM" ;) – NinethSense Jun 17 '09 at 15:19
  • 1
    @NinethSense, someone without any teaching skills? – Peter Jun 17 '09 at 15:53
  • 1
    Googleable is not a tag, and for a reason. Please don't add nonsense, useless tags. – GEOCHET Jun 17 '09 at 19:14
  • regardless of whether a question is googlable or not, the tag is useless. If you're afraid of downvotes, this may not be the correct site for you. – belgariontheking Jun 17 '09 at 19:18
  • 1
    @all, you are talking about a tag that was removed 7 hours a go, and that was not put back. I had a point with that tag that I described in a comment. All in good faith and with best intentions, but I appreciate nobody else likes that tag, so I never put it back. – Peter Jun 17 '09 at 20:00
  • @Peter, spoon feeding answers is not teaching. Showing others how to find the answer themselves is much more valuable. The whole give a man a fish, feed him for a day - teach a man to fish and feed him for a lifetime thing. I'm having trouble with your logic... why not just ask a question about every function in PHP and put a googleable tag on it? Or am I misunderstanding? – Mike B Jun 17 '09 at 23:33
  • @Mike: Ok one last time and very clear now : I think the googleable tag was a mistake and not thought trouhg. I stated in a comment myself : "Than again : since the majority of questions is googleable, the tag might not be that usefull :-) " Also in a comment I explained why I thougt first it might be usefull. (Althoug reactions as belgariontheking show its not that clear). So far for the tag now I hope. As far as the question goes : see next comment :-) – Peter Jun 18 '09 at 07:19
  • @Mike : the question is not spoonfeeding, I *am* a beginner (@php that is), and added that tag too, which was removed for unclear reasons. I didnot put that back, because I never "tagfight". I really had that question and decided not to google on it, but to SO on it, which is supposed to be OK. Only the majority of the SO userbase doesn't seem to know that. SO results are better than googles in the first place, all the relevant things are voted up, and secondly now a search on "print echo difference" should give this (good) result on it one day. It's a win win situation, but nobody – Peter Jun 18 '09 at 07:23
  • seems to care about that philosophy (which is of course not mine) – Peter Jun 18 '09 at 07:24
  • It just seems to be like you're trying to duplicate PHP's manual on SO simply because it's easier to search here than php.net. Not to mention the potential for rep whoring due to this philosophy. The differences between the 2 functions are clearly defined and cited on both pages in PHP's manual and I'm sure there are several comments making similar contributions. When you have question about a specific function, I don't understand why you would come to SO first instead of one of the best programming language manuals in the world. – Mike B Jun 18 '09 at 18:13
  • @Mike I don't even understand the repwhoring concept : it's not like it's money or anyting? Or can you cash it in somewhere, that would be nice..(for Jon S. anyway) No, I read somewhere that it was OK to use SO in stead of google (and it was about asking a new question) so I did it, end of the story. But since nobody likes a googleable question, everybody is voting down. So far for repwhoring, and so for also for the SO goals.. – Peter Jun 18 '09 at 18:26

4 Answers4

26

From this link, suggested by the PHP manual entry for the echo() function:

  1. Speed. There is a difference between the two, but speed-wise it should be irrelevant which one you use. echo is marginally faster since it doesn't set a return value if you really want to get down to the nitty gritty.

  2. Expression. print() behaves like a function in that you can do: $ret = print "Hello World"; And $ret will be

  3. That means that print can be used as part of a more complex expression where echo cannot. An example from the PHP Manual:

    $b ? print "true" : print "false";

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.

  1. Parameter(s). The grammar is: echo expression [, expression[, expression] ... ] But echo ( expression, expression ) is not valid. This would be valid: echo ("howdy"),("partner"); the same as: echo "howdy","partner";
    (Putting the brackets in that simple example serves no purpose since there is no operator precedence issue with a single term like that.)

So, echo without parentheses can take multiple parameters, which get concatenated:

echo "and a ", 1, 2, 3; // comma-separated without parentheses
echo ("and a 123"); // just one parameter with parentheses

print() can only take one parameter:

print ("and a 123"); print "and a 123";

karim79
  • 339,989
  • 67
  • 413
  • 406
4

some say echo is slightly faster than print since it has no return value. though here is someone who doesn't think the speed difference matters much... http://fabien.potencier.org/article/8/print-vs-echo-which-one-is-faster

NJChim
  • 287
  • 1
  • 2
  • 7
3

the answer is in the docs.

SilentGhost
  • 307,395
  • 66
  • 306
  • 293
2

print returns, echo does not.

And you are right, totally googleable.

anddoutoi
  • 9,973
  • 4
  • 29
  • 28