Home >Backend Development >PHP Tutorial >Why Am I Getting \'Trying to Access Array Offset on Value of Type Bool\' in PHP 7.4?

Why Am I Getting \'Trying to Access Array Offset on Value of Type Bool\' in PHP 7.4?

Linda Hamilton
Linda HamiltonOriginal
2024-11-15 08:39:02384browse

Why Am I Getting

Array Access Error in PHP 7.4: "Trying to Access Array Offset on Value of Type Bool"

When upgrading to PHP 7.4, you may encounter the error "Trying to access array offset on value of type bool." This can occur due to accessing an array key that contains a boolean value.

Consider the following example code:

public static function read($id)
{
    $Row = MySQL::query("SELECT `Data` FROM `cb_sessions` WHERE `SessionID` = '$id'", TRUE);
    // Check for null value before accessing Data key
    $session_data = $Row['Data'] ?? '';
    return $session_data;
}

In this case, the $Row['Data'] key may contain a boolean value, which will cause the error in PHP 7.4. To resolve this, you can use the null coalescing operator (??) to conditionally assign a default value if the value is null. Alternatively, you can use the null coalesce assignment operator (??=) to assign a default value and check for nullity.

The above is the detailed content of Why Am I Getting \'Trying to Access Array Offset on Value of Type Bool\' 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