Frage

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?

War es hilfreich?

Lösung

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.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top