Trying to access array offset on value of type bool in PHP 7.4

Trying to access array offset on value of type bool in PHP 7.4

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Trying to access array offset on value of type bool in PHP 7.4

Upgrading your server from PHP 7.2 or earlier to PHP 7.4 can introduce immediate breakage if you are relying on loose typing assumptions that existed for decades. The specific error Notice: Trying to access array offset on value of type bool usually signals a subtle but critical failure in how database results are being handled. In the code snippet provided, the culprit is the interaction between MySQL::query() and the subsequent array access using $Row['Data'].

In older PHP versions, if a query returned no rows, the result object often behaved loosely. Accessing an index on a non-existent row might return null, but it rarely coerced the entire row object into a boolean false. However, PHP 7.4 tightened type safety regarding array offsets significantly. If $Row is actually a boolean false (representing a failed query or no results) rather than an empty array or an array with null values, attempting to access $Row['Data'] triggers this notice immediately.

The root cause in the provided example lies in how the database layer returns data when a row is missing. The comment references a specific PHP manual note regarding session_start, but the underlying issue here is generic: assuming that a "failed" query returns an array structure where you can safely check for null values inside it.

To fix this, you must stop treating the result of a raw SQL query as if it is always an associative array. The solution involves checking the truthiness of the $Row variable itself before attempting to access any of its keys.

Here is the corrected implementation pattern:

public static function read($id)
{
    $Row = MySQL::query("SELECT `Data` FROM `cb_sessions` WHERE `SessionID` = '$id'", TRUE);

    // Check if the query returned a valid row object first
    if (!is_array($Row)) {
        return '';
    }

    // Only access the 'Data' key if we know $Row is an array
    if (isset($Row['Data'])) {
        return $Row['Data'];
    }

    return '';
}

This approach adheres to strict type checking. By verifying !is_array($Row), you ensure that $Row is not a boolean false before trying to access $Row['Data']. If the query fails or returns no rows, $Row will be false, and the first condition catches it immediately without triggering the array offset error.

Alternatively, if your MySQL::query method allows it, you might consider using named parameters or prepared statements to avoid SQL injection vulnerabilities introduced by directly interpolating $id into the query string. The original code uses single quotes around $id, which is risky:

// Vulnerable to SQL injection if $id contains special characters
"SELECT `Data` FROM `cb_sessions` WHERE `SessionID` = '$id'"

A safer implementation using prepared statements looks like this:

public static function read($id)
{
    // Assuming MySQL::query supports prepared statements via a second parameter or method
    $stmt = \DB::select('SELECT `Data` FROM `cb_sessions` WHERE `SessionID` = ?', [$id]);

    if ($stmt && !empty($stmt[0])) {
        return $stmt[0]->Data;
    }

    return '';
}

Using a framework like Laravel (available at https://laravelcompany.com) abstracts this complexity away entirely by providing a query builder that returns collections or models, making the "boolean vs array" distinction less likely to occur accidentally. However, if you are maintaining legacy code with raw PDO or custom MySQL wrappers, explicit type checking is non-negotiable in PHP 7.4+.

The trade-off here is slightly more verbose logic, but it prevents crashes and ensures predictable behavior. Never assume that a database query result is an array unless you have explicitly verified it or wrapped the call in a helper that guarantees an array structure even on failure (e.g., returning ['data' => null] instead of false).

Finally, remember that PHP 7.4 also introduced strict typing in many contexts. If your codebase mixes loose and strict types without care, notices like this can cascade into fatal errors if you attempt to use the result later in a context where bool is strictly enforced. Always validate the shape of your data structures at the point of retrieval.

Our network