简体   繁体   English

为什么分配和返回的结果不同?

[英]Why results of assign and return is different?

Result of assign of variable is differrent from return of a function: 变量赋值的结果与函数的返回结果不同:

function test() {
return !true
    or !true
    or !count(4)
    or (
        new stdClass() and true
    );
}

$result = !true
        or !true
        or !count(4)
        or (
            new stdClass() and true
        );

echo (int)$result . PHP_EOL; // 0

echo (int)test() . PHP_EOL; // 1

This is due to the Operator Precedence . 这是由于操作员优先

Assignment operation has higher Precedence than and / or . 分配操作的优先级高于and / or

First one is equal to: 第一个等于:

function test() {
return (!true
    or !true
    or !count(4)
    or (
        new stdClass() and true
    ));
}

while second one is equal to: 而第二个等于:

($result = !true)
        or !true
        or !count(4)
        or (
            new stdClass() and true
        );

Using && / || 使用&& / || instead of and / or , then the result will be same. 代替and / or ,则结果将相同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM