Fix “Passing null to parameter … is deprecated” (PHP 8.1+)
Short answer
It’s a deprecation notice from PHP 8.1 and later: a plugin or theme gives a PHP function such as htmlspecialchars(), strlen() or trim() the value null (nothing) where the function expects text or a number. The page still works, and the result is the same as before. It appears only when the value happens to be empty, for example a post without a subtitle, which is why it comes and goes. Site owners: update the plugin or send the message to its developer. Developers: give the value a default with ?? '' before the call.
The notice, on PHP 8.0 and 8.1
A reviews plugin prints each review’s title. This review has none, so the title is null:
wp-content/plugins/recent-reviews/includes/render.php
<?php
$review = ['author' => 'Sam']; // this review has no title
$title = $review['title'] ?? null;
echo '<h3>', htmlspecialchars($title), "</h3>\n";
<h3></h3><h3>
Deprecated: htmlspecialchars(): Passing null to parameter #1 ($string) of type string is deprecated in /var/www/html/wp-content/plugins/recent-reviews/includes/render.php on line 4
</h3>On both versions the heading comes out empty. PHP 8.1 adds the notice, and if your site displays PHP messages, it lands right inside the heading on the page.
php.net, in the PHP 8.1 deprecations: “Scalar types for built-in functions are nullable by default. This behaviour is deprecated to align with the behaviour of user-defined functions, where scalar types need to be marked as nullable explicitly.” (php.net)
Read the message
The message tells you exactly where to look:
- The function:
htmlspecialchars(), the PHP function that received the empty value. - The parameter:
#1 ($string), its first parameter, which expects text. - The file and line: the plugin or theme, and the line to fix.
For site owners
- Find the plugin or theme from the path in the message.
- Update it (Dashboard → Updates).
- Still there? Send the full message, and the page where it appears, to the developer.
- Meanwhile, log notices instead of displaying them, so they don’t end up inside your pages (how).
For developers: the fix
Give the value a default of the expected type before the call. Same output, no notice, on PHP 8.0, 8.1 and 8.5:
wp-content/plugins/recent-reviews/includes/render.php
<?php
$review = ['author' => 'Sam']; // this review has no title
$title = $review['title'] ?? '';
echo '<h3>', htmlspecialchars($title), "</h3>\n";
<h3></h3><h3></h3><h3></h3>For numbers, use ?? 0; for a value that may be false too, cast explicitly: (string) $value.
Why a check before upgrading can’t find it
htmlspecialchars( $title ) is correct whenever $title holds text; whether it’s null depends on each review. A check that reads code can’t know it, and CompatNav reports nothing for the example above. Your error log does, after the switch, or on a staging copy before it (how). The full background, including the PHP 9.0 plan: “Passing null to parameter … is deprecated”.
Key takeaways
- A PHP 8.1+ deprecation notice; the page works and the output is unchanged.
- It appears only when a value is empty at that moment, so it comes and goes.
- The message names the function and the parameter:
htmlspecialchars(): Passing null to parameter #1 ($string). - The developer fix:
?? ''for text,?? 0for numbers, before the call. - Because it depends on values, only the error log finds it, not a code check.
Frequently asked questions
Why does the notice appear on some pages only?
Because the value is empty only for some posts, products or visitors. The same line works whenever the value exists.
Will it become an error?
The PHP 8.1 proposal plans a TypeError in PHP 9.0. Until then, PHP 8.x only adds the notice.
Can I fix it in the theme myself?
In your own custom theme, yes, with the one-line fix below. In a theme or plugin from someone else, update it or ask its developer: your edits would be lost at the next update.
Check your own site before you upgrade
CompatNav is a free WordPress plugin. It reads the code of your plugins and themes on your own server and tells you, in plain words, what will break and what will only show notices on the PHP version you choose. It never changes your code. It can’t see problems that only appear while code runs with real data, so a quick check of your site after the upgrade still matters.
Sources
- php.net: Deprecated features in PHP 8.1.x
- PHP RFC: Deprecate passing null to non-nullable arguments of internal functions
About the code examples: each output is the real output of the code shown, run with the official PHP builds, without a php.ini, with all errors reported and displayed. Only the file path was replaced by a neutral server path.