Message: Trying to access array offset on value of type null

Message: Trying to access array offset on value of type null

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

Stefan Bogdanescu

Founder & Senior Architect

Message: Trying to access array offset on value of type null

You are encountering a strict typing error that became prevalent in PHP 7 and is enforced even more aggressively in versions like 7.4. The specific message, Trying to access array offset on value of type null, occurs when you attempt to read an element from an array using bracket notation—like $variable['key']—while the variable itself holds a null value rather than an array.

In your case, the error originates in a function within the mpdf library ($cOTLdata['char_data']). The logic attempts to check if $cOTLdata['char_data'] is null using the ternary operator before calling count(). However, this defensive coding strategy fails because the bracket notation itself is evaluated before the conditional logic can save you. When PHP sees $cOTLdata['char_data'], it immediately tries to access the offset 'char_data' on whatever type $cOTLdata currently holds. If $cOTLdata is null (which happens if a PDF operation receives no character data), PHP throws the fatal error before your conditional check can ever execute.

The Root Cause: Precedence and Evaluation Order

The issue lies in how the ternary operator interacts with bracket notation. Your current line looks like this:

$len = $cOTLdata['char_data'] === null ? 0 : count($cOTLdata['char_data']);

While the intention is sound—checking for null before counting—the syntax forces PHP to evaluate $cOTLdata['char_data'] twice. Crucially, in the first instance inside the condition $cOTLdata['char_data'] === null, PHP must resolve the array access. If $cOTLdata is null at that moment, the error triggers immediately. You cannot "short-circuit" a bracket access based on a ternary check unless you explicitly cast or verify the parent variable first.

This is a common migration trap when moving legacy scripts to PHP 7+. Older versions of PHP would silently convert null to an empty array in some contexts or simply not throw this specific error, but modern strict modes do not tolerate accessing properties or offsets on null values.

Correcting the Logic

To fix this without modifying the upstream mpdf source code (which you noted caused conflicts), you must ensure that $cOTLdata is an array before attempting to read its keys. You can achieve this by checking the type of the parent variable first using is_array().

Here is a robust replacement for your function logic:

public function trimOTLdata(&$cOTLdata, $Left = true, $Right = true)
{
    // Ensure $cOTLdata is an array before accessing 'char_data'
    if (!is_array($cOTLdata)) {
        // Handle the case where data is missing or null gracefully
        return; 
    }

    // Now it is safe to access the offset
    $charData = $cOTLdata['char_data'];
    
    // Check specifically for null on the sub-element
    $len = ($charData === null) ? 0 : count($charData);
    
    // ... rest of your logic
}

Alternatively, if you want to keep the single-line structure but avoid the bracket access on null, you can use the null coalescing operator or a separate check. However, splitting the read makes the code more readable and debuggable:

$charData = $cOTLdata['char_data'] ?? [];
$len = count($charData);

This approach uses the ?? operator to default to an empty array if 'char_data' does not exist or is null, preventing the access error entirely. Note that this requires PHP 7.0+, which aligns with your upgrade to 7.4.

Managing Library Conflicts

Since you mentioned that overwriting files from the mpdf GitHub repository did not resolve the issue, it is likely that the version of mpdf installed in your project differs from the one on the repository, or there are class aliasing conflicts preventing the new code from loading.

If you maintain the script but rely on a third-party library like mpdf, modifying core files is often a fragile solution. A better long-term strategy for an unmaintained script is to create a wrapper layer or use Composer's override mechanism if possible, ensuring your custom fixes sit above the vendor defaults. However, given that you are happy with the current script and unwilling to rewrite it, the direct type-checking fix inside the function remains the most practical path forward.

Why This Matters for Production

Ignoring this error in a production environment can lead to silent failures or partial PDF generation, as mpdf might crash midway through rendering a document. By enforcing strict type checks on inputs before accessing array offsets, you ensure that your application handles missing data gracefully rather than halting unexpectedly. This pattern of validating the container ($cOTLdata) before reading the content ('char_data') is essential when dealing with dynamic data structures in PHP 7+.

Our network