PHP: assign a variable and test it in the same if statement?

I want to do this:

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

PHPStorm warns that the $ undefined field after && & even if it was just set. Is it just a PHPStorm rough or is there some reason it really shouldn't work?

+4
source share
1 answer

This is the operatorโ€™s priority. Just take a 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
}

work as you expected. In this case, PhpStorm does not warn

, , 100% . , , PhpStorm , .

+4

Source: https://habr.com/ru/post/1540265/


All Articles