条件赋值的嵌套否定 if 语句

Nested negative if statements with assignments in condition

我有以下代码:

if (!$x = some_function(1)) {
    if (!$x = some_function(2)) {
        return something;
    }
}

我想知道下面哪些语句是等价的:

一个。

if (some_function(1)) {
    $x = some_function(1));
}
else if (some_function(2)) {
    $x = some_function(2));
}
else {
    return something;
}

或者如果它本质上是在说它应该被覆盖,就像这样:

B.

if (some_function(1)) {
    $x = some_function(1));
}
if (some_function(2)) {
    $x = some_function(2));
}
if (!$x) {
    return something;
}

问题的另一种表述方式:在 if 语句中的赋值中,变量是先为 false 求值,然后在 false 时赋值,还是赋值首先发生,然后是下一个评估的变量?

感谢 Scuzzy 的澄清——似乎正确的等价物是这样的:

if (some_function(1)) {
    $x = some_function(1));
}
if (!$x && some_function(2)) {
    $x = some_function(2));
}
if (!$x) {
    return something;
}

第一个陈述不等同于任何其他陈述。这相当于:

$x = some_function(1); // assign $x first
if(!$x){ // check if $x is falsy
  $x = some_function(2); // overwrite $x (not the function itself)
  if(!$x){ // check if $x is still falsy
   // do stuff
  }
}

或者,如果变量不重要,这也是等价的

if(!some_function(1) && !some_function(2)){...}

唯一的区别是第一个总是为 $x 提供一个值,这可能在其他地方使用。

这个也是一样的,用的是三元

$x = some_function(1) ? some_function(1) : some_function(2);
if(!$x) // do stuff