PHP - 在if语句中声明变量并在同一语句中使用它

1 php if-statement declaration php-7

我想在if语句中声明一个变量并在同一语句中使用它.这根本不可能吗?

问题的例子(虽然不是实际用例......):

if ($test = array('test'=>5) && $test['test'] === 5) {
    echo 'test';
} else {
    echo 'nope';
}
Run Code Online (Sandbox Code Playgroud)

错误信息:

注意未定义的变量:在第6行进行测试

Abr*_*ver 6

由于运算符优先级,您需要将赋值分组为():

if ( ($test = array('test'=>5) ) && $test['test'] === 5) {
    echo 'test';
} else {
    echo 'nope';
}
Run Code Online (Sandbox Code Playgroud)