PHP str_replace(): Passing null to parameter #3 ($subject) of type array|string is deprecated

PHP str_replace(): Passing null to parameter #3 ($subject) of type array|string is deprecated

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

Stefan Bogdanescu

Founder & Senior Architect

PHP str_replace(): Passing null to parameter #3 ($subject) of type array|string is deprecated

You are encountering this deprecation warning because you are running PHP 8.1 or later, a version that enforces stricter type checking on built-in functions like str_replace. The specific error message indicates that the third argument, $subject, is receiving a null value when the function signature expects either an array or a string.

In your case, the stack trace points to core/Output.php within CodeIgniter 4.1.8 at line 457. The problematic code attempts to strip out execution time and memory usage placeholders:

if ($this->parse_exec_vars === TRUE) 
{ 
    $memory = round(memory_get_usage() / 1024 / 1024, 2).'MB'; 
    // below is line 457
    $output = str_replace(array('{elapsed_time}', '{memory_usage}'), array($elapsed, $memory), $output); 
}

The root cause is usually that $output contains null. In CodeIgniter's output buffering system, if no content has been echoed yet, or if the buffer handling logic returns null under specific conditions (such as certain cache states or empty responses), $output will be null. Passing null to str_replace in PHP 8.1 triggers a deprecation warning because the function signature was updated to require a string or array, not null.

Why This Happens

Prior to PHP 8.1, functions like str_replace were more lenient and would simply treat null as an empty string. However, PHP 8 introduced nullable types for many built-in functions, but str_replace was updated to be stricter regarding its third parameter in later patches (specifically around PHP 8.1/8.2) to align with the new strict typing rules.

When $this->parse_exec_vars is true, the framework attempts to inject performance metrics into the final output. If the buffer is empty (null), the logic still tries to run str_replace, causing the crash. This often happens when debugging or during specific request lifecycle events where the output buffer hasn't been populated yet.

Fixing the Issue in CodeIgniter

The most robust solution is to ensure that $output is never null before calling str_replace. You can handle this by providing a default empty string if the value is missing. This approach prevents the deprecation warning and ensures the output buffering logic remains resilient.

Update your code to check for nullity or use the null coalescing operator:

if ($this->parse_exec_vars === TRUE) 
{ 
    $memory = round(memory_get_usage() / 1024 / 1024, 2).'MB'; 
    
    // Ensure $output is never null to satisfy strict typing requirements
    if (is_null($output)) {
        $output = '';
    }

    $output = str_replace(array('{elapsed_time}', '{memory_usage}'), array($elapsed, $memory), $output); 
}

Alternatively, you can use the ternary operator for a more concise solution:

$output = is_null($output) ? '' : $output;
$output = str_replace(array('{elapsed_time}', '{memory_usage}'), array($elapsed, $memory), $output);

Alternative Approaches

If you have control over the underlying framework logic or are writing a custom output handler, consider using mb_str_replace if your strings contain multibyte characters, though str_replace is generally sufficient for standard ASCII performance metrics.

Another option is to ensure that the output buffer is initialized before this hook runs. However, modifying core framework files in CodeIgniter 4 should be done with caution. If you are working on a production environment, it is often better to create a custom service or override the specific method in your application config rather than patching core/Output.php directly.

Broader Context for PHP 8 Upgrades

This error is part of a larger trend in PHP 8 and 8.1 where deprecation notices regarding type mismatches are becoming more common. Functions like str_replace, strstr, and others now strictly validate their inputs. If you are migrating from PHP 7.x to 8.x, you will encounter similar warnings throughout your codebase where variables that were previously "loosely" typed might now be null.

Addressing these issues early is crucial because these deprecation warnings often precede fatal errors in future PHP versions (like 8.2 or 8.3) where the behavior might change from a warning to an actual error. Proactively handling nullable inputs in string manipulation functions ensures your application remains compatible as you upgrade PHP versions.

If you are managing a larger codebase and need to audit all instances of str_replace usage, static analysis tools like PHPStan or Psalm can help identify potential nullability issues before they reach production. For teams using Laravel-based stacks, similar strictness applies, and ensuring type safety in string operations is a best practice for maintaining stability across framework updates.

Our network