Deprecated: preg_match(): Passing null to parameter #2 ($subject) of type string is deprecated

Deprecated: preg_match(): Passing null to parameter #2 ($subject) of type string is deprecated

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

Stefan Bogdanescu

Founder & Senior Architect

Deprecated: preg_match(): Passing null to parameter #2 ($subject) of type string is deprecated

You are encountering a deprecation warning because your PHP runtime version has become stricter about type enforcement, specifically regarding string parameters. The error message points directly to line 82 in C:\wamp64\scripts\config.inc.php:

if(preg_match('~^'.$key.'\s+:\s+(.+)~mi',$output,$matches) === 1) {
    $Text_Encoding[$key] = $matches[1];
}

The core issue is that the variable $output is evaluating to null when preg_match attempts to process it. In PHP versions prior to 8.1, passing a non-string value (like null) to a string parameter often resulted in no error or implicit conversion. However, modern PHP explicitly rejects this behavior to prevent subtle bugs where logic might break silently due to type mismatches.

Why $output is Null

To fix this, you must understand why $output is null at runtime. The code snippet suggests that $output is expected to contain a string representation of headers (likely from getallheaders() or similar), but under certain conditions—such as missing headers, empty responses, or specific server configurations—it returns an empty array or null depending on the environment.

When $Text_Encoding is an associative array and you are iterating over it, the regex logic assumes $output always exists as a populated string. If the upstream code generating $output failed to populate it, or if the request resulted in no headers being sent, $output remains null. Passing null into the second argument of preg_match triggers the deprecation warning because the function signature strictly requires a string.

Immediate Mitigation Strategies

The most robust fix is to ensure that the variable passed to preg_match is never null before the function call. You have three primary approaches, ranging from defensive programming to architectural changes.

1. Null Coalescing with Default Value

You can modify the regex call to use a default empty string if $output is null. This prevents the warning and ensures the regex engine has a valid string to work against.

$subject = $output ?? '';

if (preg_match('~^'.$key.'\s+:\s+(.+)~mi', $subject, $matches) === 1) {
    $Text_Encoding[$key] = $matches[1];
}

By using the null coalescing operator (??), you guarantee that $subject is always a string. If $output is null, it defaults to an empty string. The regex will simply fail to match against an empty string without raising a deprecation error, allowing the script to continue gracefully.

2. Guard Clause for Empty Data

If the logic implies that processing should only occur when headers actually exist, you can add a guard clause at the start of your loop or block:

if ($output === null) {
    return; // Or handle the missing data appropriately
}

foreach($Text_Encoding as $key => $value) {
    if(preg_match('~^'.$key.'\s+:\s+(.+)~mi',$output,$matches) === 1) {
        $Text_Encoding[$key] = $matches[1];
    }
}

This approach is slightly more performant if the null check happens frequently, as it avoids initializing a dummy string and attempting a regex match that cannot succeed anyway. However, you must ensure that returning early or skipping logic aligns with your broader application flow.

3. Explicit Type Casting

Another option is to cast $output explicitly before passing it:

$outputString = (string) $output;
if (preg_match('~^'.$key.'\s+:\s+(.+)~mi', $outputString, $matches) === 1) {
    // ...
}

This forces PHP to convert null to an empty string (''). While functionally similar to the null coalescing operator, casting makes the intent explicit in the code.

Root Cause Analysis and Best Practices

Looking at your source material, the file config.inc.php appears to be a custom configuration script within WAMP. The logic attempts to parse header strings dynamically. If this script is intended to run on every request, relying on $output without validation is risky.

The deprecation warning is a signal from the PHP engine that your code assumes a state (that $output is a valid string) which is not guaranteed by the environment. Fixing the immediate symptom by handling the null value is necessary, but you should also investigate why $output might be null in the first place. Is there a condition where headers are stripped? Is getallheaders() being called on a request that hasn't sent them yet?

In larger frameworks like Laravel, such defensive coding patterns are standard. If you are porting logic to a modern stack or migrating from legacy code, expect similar deprecation warnings related to type juggling. The PHP team is moving away from implicit conversions to make debugging easier. Ignoring these warnings in production can lead to unpredictable behavior where null values slip through and cause fatal errors later in the execution chain.

For developers managing their own server environments or custom scripts like this WAMP configuration, addressing these deprecations now ensures compatibility with future PHP versions (9.x+) where such warnings will likely become fatal errors.

Our network