PHP question mark

前端 未结 7 1489
北荒
北荒 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 02:53

    Actually this statment is representing a Ternary operation, a conditional expression:

    // works like:    (condition) ? if-true : if-false;
    
    $hideCode = $likesObj->isAlreadyLikedByUser(facebookUID()) ?  'style="display:none;"':'';
    

    in your case $hideCode will have style="display:none;" value if

    $likesObj->isAlreadyLikedByUser(facebookUID())
    

    will return true, else it will be null or empty.

    0 讨论(0)
  • 2020-12-31 02:54

    $hideCode = $likesObj->isAlreadyLikedByUser(facebookUID()) ? 'style="display:none;"':'';

    This is the same as the following:

    if ($likesObj->isAlreadyLikedByUser(facebookUID()))
    {
        $hideCode = 'style="display:none;"';
    }
    else
    {
        $hideCode = '';
    }
    
    0 讨论(0)
  • 2020-12-31 02:57

    It's a shorter version of a IF statement.

    $hideCode = $likesObj->isAlreadyLikedByUser(facebookUID()) ? ' style="display:none;"':'';
    

    if in fact :

    if($likesObj->isAlreadyLikedByUser(facebookUID()))
    {
       $hideCode = 'style="display:none"';
    }
    else
    {
     $hideCode = "";
    }
    

    For the purism :

    It's representing a Ternary operation

    0 讨论(0)
  • 2020-12-31 03:03

    This is the simple if-then-else type logic:

    (condition) ? (if-true-value) : (if-false-value)
    

    so in your case, the condition is checked (i.e. has the page already been liked by the user); if yes (true condition), then style="display:none;" is printed so that whatever the element you're using is not displayed. Otherwise, an empty string is printed, which is the equivalent of not printing anything at all, naturally.

    0 讨论(0)
  • 2020-12-31 03:10

    It's part of a ternary operator. The first part is the conditional of an if-else statement. After the question mark is the "if" block, and after the colon is the "else" block.

    0 讨论(0)
  • 2020-12-31 03:15

    It is the ternary operator: it means

    if $likesObj->isAlreadyLikedByUser(facebookUID()) is true assign style="display:none; to the variable, otherwise assign ''

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