Home >Backend Development >PHP Tutorial >Why Am I Getting \'Error Trapped: Array Offset Access on Boolean Value\' in PHP 7.4?

Why Am I Getting \'Error Trapped: Array Offset Access on Boolean Value\' in PHP 7.4?

Patricia Arquette
Patricia ArquetteOriginal
2024-11-15 04:18:02588browse

Why Am I Getting

Error Trapped: Array Offset Access on Boolean Value in PHP 7.4

Query:

After upgrading to PHP 7.4.1, the following error occurs:

Notice: Trying to access array offset on value of type bool in

within this code block:

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(is_null($Row['Data']))
    {
        $session_data = '';
    }
    else
    {
        $session_data = $Row['Data'];
    }
    
    return $session_data;
}

Solution:

PHP 7.4 enforces stricter type checking, causing this error when $Row['Data'] might be null. To resolve this, use the null coalescing operator (??) to assign a default value if $Row['Data'] is null:

return $Row['Data'] ?? 'default value';

Alternatively, you can use the null coalescing assignment operator (??=) to set the default value if $Row['Data'] is null:

$Row['Data'] ??= 'default value';
return $Row['Data'];

The above is the detailed content of Why Am I Getting \'Error Trapped: Array Offset Access on Boolean Value\' in PHP 7.4?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn