PHP错误与三元运算符? [英] PHP bug with ternary operator?

查看:96
本文介绍了PHP错误与三元运算符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

function foo() {
    return $result = bar() ? $result : false;
}

function bar() {
    return "some_value";
}

foo();

注意:未定义的变量:结果

Notice: Undefined variable: result

这是一个错误吗?

bar()应该保存到$ result,但不是.但是条件很好,它试图返回$ result或false语句(以防bar()为NULL或false)

bar() should be saved to $result, but it doesn't. However the condition works fine and it's trying to return $result or false statement (in case bar() is NULL or false)

PHP 5.4.24

PHP 5.4.24

推荐答案

这是因为-因此分配的优先级更高.

-so assignment will be evaluated with higher precedence.

这篇关于PHP错误与三元运算符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆