PHP 返回(值);vs 返回值;
return($var);
和 return $var;
除了用括号括起来有什么区别吗?
Is there any difference between return($var);
and return $var;
other then wrapping it in parentheses?
推荐答案
除非你是通过引用返回,他们的意思是一样的.最好排除括号.来自文档:
Unless you are returning by reference, they mean the same thing. It is preferable to exclude the parentheses. From the docs:
注意:请注意,由于 return()
是一种语言结构而不是函数,因此不需要在其参数周围加上括号.将它们排除在外是很常见的,实际上您应该这样做,因为在这种情况下 PHP 要做的工作较少.
Note: Note that since
return()
is a language construct and not a function, the parentheses surrounding its arguments are not required. It is common to leave them out, and you actually should do so as PHP has less work to do in this case.
注意:当通过引用返回时,你不应该在你的返回变量周围使用括号,因为这不起作用.您只能通过引用返回变量,而不是语句的结果.如果你使用 return ($a)
;那么您返回的不是变量,而是表达式 ($a)
的结果(当然,也就是 $a
的值).
Note: You should never use parentheses around your return variable when returning by reference, as this will not work. You can only return variables by reference, not the result of a statement. If you use
return ($a)
; then you're not returning a variable, but the result of the expression($a)
(which is, of course, the value of$a
).
相关文章