【问题标题】:shorthand if explanation ? mark operator如果解释的速记?标记运算符
【发布时间】:2016-01-27 12:39:38
【问题描述】:

对于模板引擎,我想使用 if 条件的简写。 我需要检查一个值是否为 != null 如果为真则打印出一些行。 我尝试了什么:

echo "test" ($user->affiliate_id !=null) ?   

我不知道在后面写什么?

【问题讨论】:

标签: php shorthand-if


【解决方案1】:

$someVariable = $condition ? $valueA : $valueB 行相当于:

if ( $condition ) {
    $someVariable = $valueA;
}
else {
    $someVariable = $valueB;
}

所以,基本上,如果条件是TRUE$someVariable 将采用? 符号之后的第一个值。如果是FALSE,则取第二个值(: 符号后面的那个)。

有一种特殊情况,您可以定义第一个值,就像这样:$someVariable = $someValue ?: $someOtherValue。相当于:

if ( $someValue ) {
    $someVariable = $someValue;
}
else {
    $someVariable = $someOtherValue;
}

因此,如果$someValue 计算为TRUE(任何不同于0 的值都计算为TRUE),那么$someVariable 将捕获该值。否则,它将捕获$someOtherValue

举个例子:

function printGender( $gender ) {
    echo "The user's gender is: " . ( $gender == 0 ? "Male" : "Female" );
}

printGender(0); // Will print "The user's gender is: Male"
printGender(1); // Will print "The user's gender is: Female"

另一个例子:

function printValuesStrictlyDifferentThanZero( $value ) {
    echo "Value: " . ( $value ?: 1 );
}

printValuesStrictlyDifferentThanZero(0); // $value evaluates to FALSE, so it echoes 1
printValuesStrictlyDifferentThanZero(1); // $value evaluates to TRUE, so it echoes that $value

编辑:

运算符?: 称为ternary operator。有多种方法可以定义三元运算符(采用三个操作数的运算符)。它是一个三元运算符,但不是三元运算符。有些人只是称它为三元运算符,因为他们习惯于这样做,而且它可能是 PHP 中唯一广为人知的三元运算符,但三元运算符比这更通用。

它的名字是条件运算符,或者更严格地说,是三元条件运算符

假设我定义了一个名为log base 的新运算符,它计算内联,如果一个数字$A 的对数以$C 为基数等于$B,其语法类似于$correct = $A log $B base $C,它返回@如果对数正确则为 987654345@,否则为 FALSE

当然该操作是假设的,但它三元运算符,就像?:。我将其称为对数检查运算符

【讨论】:

    【解决方案2】:

    语法应该是这样的

    echo (condition) ? write if true code part here : write else part here

    这意味着在你的情况下它会像

    echo ($user->affiliate_id !=null) ? 'test' : 'not null'
    

    【讨论】:

      【解决方案3】:

      正如@Federinco 所说,它被称为三元运算符,官方文档在这里:http://php.net/manual/en/language.operators.comparison.php#language.operators.comparison.ternary

      三元运算符是这样工作的:

      condition ? true branch : false branch

      所以一个例子是:

      $cost = isset($discount) ? $total - $discount : $total;

      在上面的示例中,我们检查是否设置了折扣,如果设置了,我们从当前总计中删除折扣,如果没有,我们只需将成本设置为总计。

      您可以使用三元运算符做其他很酷的事情,如下所示:

      $name = $username ?: 'Guest';

      在上面的代码中,我们检查$username是否存在,如果存在,我们将$name设置为$username,否则设置$name等于Guest

      但请注意,如果滥用三元运算符,可能会导致可读性问题,因此在使用它时要小心,如果三元运算符不打算使用,请不要牺牲标准条件语句的额外行清楚它在做什么。

      【讨论】:

      • 实际上叫做条件运算符。是的,它是一个三元运算符(因为它需要三个操作数)。当用作二元运算符(两个操作数)时,它也称为 Elvis 运算符?: 将其视为文本表情符号)。
      【解决方案4】:

      试试这个:

      echo ($user->affiliate_id != null )?"test":"";
      

      【讨论】:

        【解决方案5】:

        你想要的是ternary operator

        您的代码应如下所示

        echo "test" . (($user->affiliate_id !=null) ? 'stringToOutput ifNotNull' : 'stringToOutput if is null');
        

        另外,PHP 7 引入了您可以使用的Null coalescing operator。你可以这样做

        echo 'test' . ($user->affiliate_id ?? 'ID not found!');
        

        在这种情况下,如果 $user->affiliate_id 被设置并且不为空,它将被打印 而不是“找不到 ID!”留言

        【讨论】:

          猜你喜欢
          • 2011-12-08
          • 1970-01-01
          • 2012-02-11
          • 2018-07-22
          • 1970-01-01
          • 2011-08-19
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多