简体   繁体   English

PHP是否与|| =运算符等效?

[英]Does PHP have an equivalent to the ||= operator?

I'm wanting to assign a variable only it hasn't already been assigned. 我想分配一个变量,只有它尚未被分配。 What's the PHP way of doing the following? PHP执行以下操作的方式是什么?

$result = null;
$result ||= check1();
$result ||= check2();
$result ||= "default";

I checked the standard operators and the is_null function, but there doesn't seem to be an easy way of doing the above operation. 我检查了标准运算符is_null函数,但似乎没有一种简单的方法可以执行上述操作。

isset() is the usual way of doing this: isset()是执行此操作的常用方法:

if (!isset($blah)) {
  $blah = 'foo';
}

Note: you can assign null to a variable and it will be assigned. 注意:您可以将null分配给变量,并且该变量将被分配。 This will yield different results with isset() and is_null() so you need to be clear on what you mean by "not assigned". 这将与isset()is_null()产生不同的结果,因此您需要清楚“未分配”的含义。 See Null vs. isset() . 参见Null与isset() This is also one case where you need to be careful about doing automatic type conversion, meaning using the !=/== or ===/!== depending on the desired result. 在这种情况下,您也需要小心进行自动类型转换,这意味着根据所需结果使用!=/=====/!==

You can use boolean shorthand for this too (which is what the Perl ||= operator is). 您也可以为此使用布尔简写(这就是Perl ||=运算符)。 As of PHP 5.2.x there is no operator like you speak of. 从PHP 5.2.x开始,没有像您所说的运算符。 In Perl: 在Perl中:

$a ||= $b;

is equivalent to: 等效于:

$a = $a || $b;

You can do the second form in PHP but PHP has some funky rules about type juggling . 您可以在PHP中执行第二种形式,但是PHP具有一些有关类型变戏法的时髦规则。 See Converting to boolean : 请参见转换为布尔值

When converting to boolean , the following values are considered FALSE: 转换为boolean时 ,以下值为FALSE:

Every other value is considered TRUE (including any resource). 其他所有值均视为TRUE(包括任何资源)。

So after: 所以之后:

$a = 0;
$a = $a || 5;

$a would equal 5. Likewise: $ a等于5。同样:

$a = 0;
$b = '';
$c = $a == $b; // true
$d = $a === $b; // false

You have to watch out for these things. 您必须提防这些事情。

isset($foo) || $foo = 'bar';

从php 5.3开始,您可以使用:

$result = $result ?: check1();

I came up with this function which has a side benefit of shutting up useless undefined variable notices. 我想到了这个功能,它具有关闭无用的未定义变量通知的副作用。 It's close enough to Javascript's behaviour for me: 对我来说,它足够接近Javascript的行为:

function default_value(&$var, $default) {
    return isset($var) ? $var : $default;
}

$want_something = default_value($_POST['mightbeunset'], false);

How about using a couple of nested ternary operators? 如何使用几个嵌套的三元运算符?

For me it's the easiest way to deal with your problem. 对我来说,这是处理您的问题的最简单方法。 And since you can use nested ternary operators you should be able to add more checks if needed. 并且由于您可以使用嵌套三元运算符,因此您应该能够在需要时添加更多检查。

$var = isset($var) ? $var : ($i=check1()) ? $i : ($i=check2()) ? $i : "default" ;

It's almost the same amount of code and it should work.. unless I've misunderstood you. 几乎是相同数量的代码,它应该可以工作..除非我误解了您。 If that's the case I'm sorry. 如果是这样,我很抱歉。

In comparison with the original code you posted as example, the length is almost the same: 与您作为示例发布的原始代码相比,其长度几乎相同:

$result = null; $result ||= check1(); $result ||= check2(); $result ||= "default";
 - vs -
$var = isset($var) ? $var : ($i=check1()) ? $i : ($i=check2()) ? $i : "default" ;

Nested ternary operators can be a little bit confusing, so here you have it with comments: 嵌套的三元运算符可能会有些混乱,因此在此处带有注释:

    // Is $var set?
    $var = isset($var) ?
        // TRUE: It is, use $var
        $var :
        // FALSE: $var is not set, run check1
        ( $i=check1() ) ?
        // TRUE: the function check1 returned valid data
        $i :
        // FALSE: the function check1 returned null or false, run check2
        ($i=check2()) ?
        // TRUE:  the function check2 returned valid data
        $i :
        // FALSE: the function check1 returned null or false, set default value
        "default" ;

I haven't tried it yet, but have wanted to play with PECL Operator for ages. 我还没有尝试过,但是想和PECL Operator玩很久了。

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

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