json_decode在内部如果条件返回1

jar*_*ack 0 php json

这是我的代码:

if(
    $jsonObj = json_decode($someJson) &&
    json_last_error() == JSON_ERROR_NONE
) {

    print_r($jsonObj);
}
Run Code Online (Sandbox Code Playgroud)

输出是1.另一种写作方式:

$jsonObj = json_decode($someJson);

if(
    $jsonObj &&
    json_last_error() == JSON_ERROR_NONE
) {

    print_r($jsonObj);
}
Run Code Online (Sandbox Code Playgroud)

输出是stdClass(我想要的).

为什么第一个代码块与第二个代码块不一样?也许只是写它就好了吗?:

$jsonObj = json_decode($someJson);

if(json_last_error() == JSON_ERROR_NONE) {

    print_r($jsonObj);
}
Run Code Online (Sandbox Code Playgroud)

Sea*_*ght 7

由于运算符优先级.布尔AND(&&)的优先级高于赋值(=),因此第一个语句是有效的:

$jsonObj = (json_decode($someJson) && json_last_error() == JSON_ERROR_NONE)
Run Code Online (Sandbox Code Playgroud)

您需要添加括号以获得所需的结果:

($jsonObj = json_decode($someJson)) && json_last_error() == JSON_ERROR_NONE
Run Code Online (Sandbox Code Playgroud)