差异

2010-08-22 21 views
8

可能重复:
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?差异

有几种方法来打印输出PHP;包括但不限于:

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

这四个之间有什么区别吗?另外,圆括号是否有所作为?

+0

为什么所有的“重复”答案都会导致“尽可能地重复答案”? – Bonzo 2012-06-13 18:06:32

回答

1

两点区别:

print有返回值(通常为1),echo没有。因此print可以用作表达式。

echo接受多个参数。所以你可以写echo $a, $b而不是echo $a . $b

关于括号:他们只是错误在我眼里。他们根本没有功能。你也可以写echo (((((((((($a))))))))));人们通常包含无知的括号,认为print是一个函数。此外,它增加了误解的可能性。例如print("foo") && print("bar")不打印foobar,因为PHP将其解释为print(("foo") && print("bar"))。因此bar1将被打印,即使它看起来不同。

+0

我知道Python不是PHP,但他们[禁止](http://docs.python.org/release/3.0.1/whatsnew/3.0.html#print-is-a-function)使用'print'东西',现在你总是必须在括号中使用print('something')'。 – Aillyn 2010-08-22 18:44:19

+0

另请参阅:http://www.ideone.com/LUOOG – Aillyn 2010-08-22 18:57:10

+0

不,它确实有所作为。即使它输出相同的版本与括号*看起来*不同。圆括号和格式化功能使它看起来不同。如果我没有停下来思考,至少我会被愚蠢地认为它会输出“foobar”。 – NikiC 2010-08-22 19:00:45