Home > Backend Development > PHP Tutorial > How to Fix \'Trying to Access Array Offset on Value of Type Bool\' Error in PHP 7.4?

How to Fix \'Trying to Access Array Offset on Value of Type Bool\' Error in PHP 7.4?

DDD
Release: 2024-11-19 16:34:02
Original
228 people have browsed it

How to Fix

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

When upgrading to PHP 7.4, some code may encounter the error "Trying to access array offset on value of type bool." This error arises when accessing an array key that corresponds to a boolean value.

One solution to this issue is to use the null coalescing operator (??), introduced in PHP 7.0. The null coalescing operator evaluates the left expression and returns its value if it's not null. Otherwise, it returns the value of the right expression.

In the provided code, the issue occurs in the following line:

if (is_null($Row['Data'])) {
    $session_data = '';
} else {
    $session_data = $Row['Data'];
}
Copy after login

To resolve the error, replace the code with the following:

$session_data = $Row['Data'] ?? '';
Copy after login

Alternatively, you can use the null coalescing assignment operator (??=) to avoid the need for an additional line:

$session_data = $Row['Data'] ??= '';
Copy after login

These modifications ensure that $session_data contains the array value if it exists or a default value (in this case, an empty string) if the array key corresponds to null.

The above is the detailed content of How to Fix \'Trying to Access Array Offset on Value of Type Bool\' Error in PHP 7.4?. For more information, please follow other related articles on the PHP Chinese website!

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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template