PHP question mark

前端 未结 7 1490
北荒
北荒 2020-12-31 02:29
$hideCode = $likesObj->isAlreadyLikedByUser(facebookUID()) ? \'style=\"display:none;\"\' : \'\';

Can anyone explain to me what that question mar

相关标签:
7条回答
  • 2020-12-31 03:18

    This is called the Ternary Operator, and it's common to several languages, including PHP, Javascript, Python, Ruby...

    $x = $condition ? $trueVal : $falseVal;
    
    // same as:
    
    if ($condition) {
        $x = $trueVal;
    } else {
        $x = $falseVal;
    }
    

    One very important point to note, when using a ternary in PHP is this:

    Note: Please note that the ternary operator is a statement, and that it doesn't evaluate to a variable, but to the result of a statement. This is important to know if you want to return a variable by reference. The statement return $var == 42 ? $a : $b; in a return-by-reference function will therefore not work and a warning is issued in later PHP versions. source

    0 讨论(0)
提交回复
热议问题