Code

Coding, Programming & Algorithms, Tips, Tweaks & Hacks
Search

PHP 7's Null Coalescing Operator

PHP 7's Null coalescing operator is more useful than I thought.

PHP
public static function read($id)
{
    $Row = MySQL::query("SELECT `Data` FROM `cb_sessions` WHERE `SessionID` = '$id'", TRUE);    
    
    # http://php.net/manual/en/function.session-start.php#120589
    //check to see if $session_data is null before returning (CRITICAL)                        
    
    if($Row['Data'] == false || is_null($Row['Data']))
    {
        $session_data = '';
    }
    else
    {
        $session_data = $Row['Data'];
    }
    
    return $session_data;    
}
PHP 7.4
can be replaced with :
PHP
public static function read($id)
{
    $Row = MySQL::query("SELECT `Data` FROM `cb_sessions` WHERE `SessionID` = '$id'", TRUE);    

    # Introduced in PHP 7 : https://stackoverflow.com/a/59687793/126833
    return $Row['Data'] ?? '';    
}
PHP 7.4
Vanakkam !

0 comments: