PHP ternary operator vs null coalescing operator

后端 未结 13 1707
南笙
南笙 2020-11-22 15:58

Can someone explain the differences between ternary operator shorthand (?:) and null coalescing operator (??) in PHP?

When do they behave d

13条回答
  •  心在旅途
    2020-11-22 16:22

    Ran the below on php interactive mode (php -a on terminal). The comment on each line shows the result.

    var_export (false ?? 'value2');   // false
    var_export (true  ?? 'value2');   // true
    var_export (null  ?? 'value2');   // value2
    var_export (''    ?? 'value2');   // ""
    var_export (0     ?? 'value2');   // 0
    
    var_export (false ?: 'value2');   // value2
    var_export (true  ?: 'value2');   // true
    var_export (null  ?: 'value2');   // value2
    var_export (''    ?: 'value2');   // value2
    var_export (0     ?: 'value2');   // value2
    

    The Null Coalescing Operator ??

    • ?? is like a "gate" that only lets NULL through.
    • So, it always returns first parameter, unless first parameter happens to be NULL.
    • This means ?? is same as ( !isset() || is_null() )

    Use of ??

    • shorten !isset() || is_null() check
    • e.g $object = $object ?? new objClassName();

    Stacking Null Coalese Operator

            $v = $x ?? $y ?? $z; 
    
            // This is a sequence of "SET && NOT NULL"s:
    
            if( $x  &&  !is_null($x) ){ 
                return $x; 
            } else if( $y && !is_null($y) ){ 
                return $y; 
            } else { 
                return $z; 
            }
    

    The Ternary Operator ?:

    • ?: is like a gate that lets anything falsy through - including NULL
    • Anything falsy: 0, empty string, NULL, false, !isset(), empty()
    • Same like old ternary operator: X ? Y : Z
    • Note: ?: will throw PHP NOTICE on undefined (unset or !isset()) variables

    Use of ?:

    • checking empty(), !isset(), is_null() etc
    • shorten ternary operation like !empty($x) ? $x : $y to $x ?: $y
    • shorten if(!$x) { echo $x; } else { echo $y; } to echo $x ?: $y

    Stacking Ternary Operator

            echo 0 ?: 1 ?: 2 ?: 3; //1
            echo 1 ?: 0 ?: 3 ?: 2; //1
            echo 2 ?: 1 ?: 0 ?: 3; //2
            echo 3 ?: 2 ?: 1 ?: 0; //3
        
            echo 0 ?: 1 ?: 2 ?: 3; //1
            echo 0 ?: 0 ?: 2 ?: 3; //2
            echo 0 ?: 0 ?: 0 ?: 3; //3
    
        
            // Source & Credit: http://php.net/manual/en/language.operators.comparison.php#95997
       
            // This is basically a sequence of:
    
     
            if( truthy ) {}
            else if(truthy ) {}
            else if(truthy ) {}
            ..
            else {}
    

    Stacking both, we can shorten this:

            if( isset($_GET['name']) && !is_null($_GET['name'])) {
                $name = $_GET['name'];
            } else if( !empty($user_name) ) {
                 $name = $user_name; 
            } else {
                $name = 'anonymous';
            }
    
    

    To this:

            $name = $_GET['name'] ?? $user_name ?: 'anonymous';
    

    Cool, right? :-)

提交回复
热议问题