What does this mean? - if (null === ($bar = $foo->getBar()))
When we have something like:
开发者_如何学Cif (null === ($bar = $foo->getBar())) {
}
Are we doing three things on this single line ? Are we:
1) Declaring a variable.
2) Attribute that variable a value.
3) Check if that variable value is null.
?
Yup, exactly like:
$bar = $foo->getBar();
if (null === $bar) {
}
$bar
will receive the value returned by $foo->getBar()
, and then the expression tests whether that (the result of the assignment expression, which is the value that got assigned to $bar
) is === null
. (And if this is the first use of $bar
, then it's creating a new variable.)
First you execute function getBar() which returns something what is assigned to variable $bar. Then operator === returns true if $bar is equal to null and they are the same type (null type).
http://www.php.net/manual/en/language.operators.comparison.php
http://www.php.net/manual/en/language.types.null.php
it's equal to:
$bar = $foo->getBar();
if ($bar === null) {
}
Keep in mind that there is difference between === and ==. === is exact comparison operator, so 0 ==null is true but 0 === null is false. "" == null is true, "" === null is false.
精彩评论