質問

I want to do this:

if ($field = myFunction($node, 'field') && $field['value']) {
  //do something with $field
}

PHPStorm warns that $field is undefined after the && even though it was just set. Is this just PHPStorm being flaky or is there some reason this actually shouldn't work?

役に立ちましたか?

解決

It's operator precedence. Just look at http://docs.php.net/manual/en/language.operators.precedence.php

It should be:

if (($field = myFunction($node, 'field')) && $field['value']) {
  //do something with $field
}

to work as you expected. In this case PhpStorm doesn't warn

Such cases show that it's better always use parentheses if you are not 100% sure about operation order. Imagine what could happen if PhpStorm doesn't warn you in this case and in your tests it was working as expected.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top