How do I use the ternary operator ( ? : ) in PHP as a shorthand for “if / else”?

后端 未结 14 1120
我在风中等你
我在风中等你 2020-11-22 08:34

Based on the examples from this page, I have the working and non-working code samples below.

Working code using if statement:

if (!empty         


        
相关标签:
14条回答
  • 2020-11-22 09:14

    If first variable($a) is null, then assign value of second variable($b) to first variable($a)

     $a = 5;
     $b = 10;   
    
     $a != ''?$a: $a = $b;
    
     echo $a;
    
    0 讨论(0)
  • 2020-11-22 09:15

    You can do this even shorter by replacing echo with <?= code ?>

    <?=(empty($storeData['street2'])) ? 'Yes <br />' : 'No <br />'?>

    This is useful especially when you want to determine, inside a navbar, whether the menu option should be displayed as already visited (clicked) or not:

    <li<?=($basename=='index.php' ? ' class="active"' : '')?>><a href="index.php">Home</a></li>

    0 讨论(0)
  • 2020-11-22 09:19

    Ternary Operator is basically shorthand for if/else statement. We can use to reduce few lines of code and increases readability.

    Your code looks cleaner to me. But we can add more cleaner way as follows-

    $test = (empty($address['street2'])) ? 'Yes <br />' : 'No <br />';

    Another way-

    $test = ((empty($address['street2'])) ? 'Yes <br />' : 'No <br />');

    Note- I have added bracket to whole expression to make it cleaner. I used to do this usually to increase readability. With PHP7 we can use Null Coalescing Operator / php 7 ?? operator for better approach. But your requirement it does not fit.

    0 讨论(0)
  • 2020-11-22 09:22

    There's also a shorthand ternary operator and it looks like this:

    (expression1) ?: expression2 will return expression1 if it evaluates to true or expression2 otherwise.

    Example:

    $a = 'Apples';
    echo ($a ?: 'Oranges') . ' are great!';
    

    will return

    Apples are great!
    

    Since PHP 5.3, it is possible to leave out the middle part of the ternary operator. Expression expr1 ?: expr3 returns expr1 if expr1 evaluates to TRUE, and expr3 otherwise.

    From the PHP Manual

    0 讨论(0)
  • 2020-11-22 09:23

    Quick and short way:

    echo $address['street2'] ? : "No";
    

    Here are some interesting examples, with one or more varied conditions.

    $color = "blue";
    
    // Condition #1 Show color without specifying variable 
    echo $color ? : "Undefined";
    echo "<br>";
    
    // Condition #2
    echo $color ? $color : "Undefined";
    echo "<br>";
    
    // Condition #3
    echo ($color) ? $color : "Undefined";
    echo "<br>";
    
    // Condition #4
    echo ($color == "blue") ? $color : "Undefined";
    echo "<br>";
    
    // Condition #5
    echo ($color == "" ? $color : ($color == "blue" ? $color : "Undefined"));
    echo "<br>";
    
    // Condition #6
    echo ($color == "blue" ? $color : ($color == "" ? $color : ($color == "" ? $color : "Undefined")));
    echo "<br>";
    
    // Condition #7
    echo ($color != "") ? ($color != "" ? ($color == "blue" ? $color : "Undefined") : "Undefined") : "Undefined";
    echo "<br>";
    
    0 讨论(0)
  • 2020-11-22 09:27

    Conditional Welcome Message

    echo 'Welcome '.($user['is_logged_in'] ? $user['first_name'] : 'Guest').'!';
    

    Nested PHP Shorthand

    echo 'Your score is:  '.($score > 10 ? ($age > 10 ? 'Average' : 'Exceptional') : ($age > 10 ? 'Horrible' : 'Average') );
    
    0 讨论(0)
提交回复
热议问题