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
Release: 2024-11-15 04:18:02
Original
597 people have browsed it

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
Copy after login

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;
}
Copy after login

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';
Copy after login

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'];
Copy after login

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!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template