Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Differences between echo, echo(), print and print() in PHP [duplicate]

Tags:

php

echo

printing

Possible Duplicates:
How are echo and print different in PHP?
Is there any difference between ‘print’ and ‘echo’ in PHP?
What’s the difference of echo,print,print_r in PHP?

There are several ways to print output in PHP; including but not limited to:

echo 'Hello';
echo ('Hello');
print 'Hello';
print ('Hello');

Are there any differences between these four? Also, do the parentheses make any difference at all?

like image 375
Aillyn Avatar asked Aug 22 '10 18:08

Aillyn


1 Answers

Two differences:

print has a return value (always 1), echo doesn't. Therefore print can be used as an expression.

echo accepts multiple arguments. So you may write echo $a, $b instead of echo $a . $b.

Concerning the parentheses: They are simply wrong in my eyes. They have no function at all. You could as well write echo (((((((((($a)))))))))); people usually include parentheses from ignorance, thinking that print is a function. Furthermore it increases the chance of misinterpretation. For example print("foo") && print("bar") does not print foobar, because PHP interprets this as print(("foo") && print("bar")). So bar1 would be printed, even though it looks different.

like image 104
NikiC Avatar answered Sep 30 '22 17:09

NikiC