0

Possible Duplicate:
What is basic difference echo Vs print

The echo and print statements are nearly identical, but they have some differences. For example, the print statement returns a value of 1 if it is successful or a value of 0 if it is not successful, while the echo statement does not return a value.

why print statement returns a value of 1 if it is successful. but echo doesn't. thank you

Community
  • 1
  • 1
runeveryday
  • 2,751
  • 4
  • 30
  • 44
  • 1
    Both are language constructs. `print` behaves like a function/expression, while `echo` may only be used as statement. – mario Aug 17 '11 at 01:28
  • If I understand the question correctly, maybe this was done by design with echo being a sort of use and forget operation. @mario: Could you go into more detail into what you mean by echo being only used as a statement. – James P. Aug 17 '11 at 01:30
  • `echo` accepts a variable number of arguments to be printed out. But it only does so when used without parenthesis. And this would be ambigious and unparseable in an expression. That's why `echo` can only be used as statement (meaning only command in a line). - More links: http://stackoverflow.com/questions/3542745/differences-between-echo-echo-print-and-print-in-php – mario Aug 17 '11 at 01:36

1 Answers1

1

I've actually taken advantage of the return value of the print "function" in an ajax call:

return print json_encode($my_data);

It doesn't do anything at all with the return value, but it terminates the execution of the current script, which is a slightly prettier way of writing

echo json_encode($my_data);
die();

But as to why one returns something and the other doesn't.... probably isn't a terrible good reason for it. I reckon echo is ever so slightly (negligibly) faster because of it, whereas print has weird uses such as the aforementioned one.

As to what these other guys are saying about print() not being a language construct, but a function, I say to you, read the manual. It's a language construct too.

mpen
  • 272,448
  • 266
  • 850
  • 1,236