首頁 >後端開發 >php教程 >為什麼我在 PHP 7.4 中收到「嘗試存取 Bool 類型值的陣列偏移量」?

為什麼我在 PHP 7.4 中收到「嘗試存取 Bool 類型值的陣列偏移量」?

Linda Hamilton
Linda Hamilton原創
2024-11-15 08:39:02385瀏覽

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.

以上是為什麼我在 PHP 7.4 中收到「嘗試存取 Bool 類型值的陣列偏移量」?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn