Home/ Engineering Insights/ Deprecated: Passing null to Parameter of Type string in PHP 8.1: What It Means and How to Fix It

Deprecated: Passing null to Parameter of Type string in PHP 8.1: What It Means and How to Fix It

Source. This article is based on PHP manual: Deprecated features in PHP 8.1.x. Details were checked against it on November 4, 2026; check the source for later changes.

You upgrade from PHP 7.4 or 8.0 to 8.1 or later, and the log starts filling with lines such as:

PHP Deprecated:  trim(): Passing null to parameter #1 ($string) of type string is deprecated in /var/www/site/form.php on line 27

The PHP 8.1 deprecation notes explain it. Scalar types of built-in functions were nullable by default, and the manual says "this behaviour is deprecated" to align with user-defined functions, where a scalar type has to be marked nullable explicitly. Its example is str_contains("foobar", null), which now reports that passing null to parameter #2 is deprecated.

What it means for your site

It is a deprecation, not an error: the call still runs and the null is treated as an empty string or zero. Nothing breaks today. But deprecated behaviour is behaviour PHP is moving away from, and on a legacy site it can produce thousands of log lines a day, hide real problems, and show up as visible text on pages that have display_errors on.

You will only see it on 8.1 or later

This message does not exist before PHP 8.1, so a staging copy on PHP 7.4 or 8.0 will not show it, and you cannot clear it on the old version first. Plan a pass on 8.1 or later with full reporting on:

php.ini on the staging copy
error_reporting = E_ALL
display_errors = Off
log_errors = On

Then count where the messages come from, ranked by file and line:

grep "Passing null to parameter" php-error.log | sed -E 's/.* in (.*) on line ([0-9]+).*/\1:\2/' | sort | uniq -c | sort -rn | head -40

Where it comes from

  • Missing input. trim($_POST['name']) when the field was not submitted.
  • Database NULLs. A nullable column read with htmlspecialchars($row['bio']) or strlen($row['phone']).
  • Functions that return null, or an unset variable passed straight to explode(), str_replace() or strpos().
  • Old plugins and themes. Third-party code that nobody on your side wrote can be the biggest source.

The fixes, in order of preference

  1. Give the value a default where it is read. With PHP 7.0 or later: trim($_POST['name'] ?? ''). On PHP 5.6, use isset() with a ternary.
  2. Cast when null genuinely means empty. htmlspecialchars((string) $row['bio']). Cast only scalars: casting an array to a string produces its own warning.
  3. Fix the data. If a column should never be null, give it a default and clean the existing rows instead of casting everywhere.
  4. Fix a repeated pattern once. If one helper is called from hundreds of places, repair the helper.
// before
$name = trim($_POST['name']);
echo htmlspecialchars($row['bio']);

// after
$name = trim($_POST['name'] ?? '');
echo htmlspecialchars((string) $row['bio']);

Prioritise by how often each line fires

The grep above gives you a ranked list. On most legacy sites a short list of lines produces the bulk of the messages, because they sit in a header, a footer or a loop that runs on every request. Fix the top ten first and re-run the count. The built-in functions involved are mostly the string workhorses: strlen(), trim(), strpos(), str_replace(), explode(), htmlspecialchars() and json_decode(). Anything that takes a string parameter can trigger it.

For third-party code, look for a newer release before you edit anything. Changes made inside a plugin or vendor folder are overwritten the next time it is updated, so if a patch is unavoidable, keep a note of it and of the version it applies to.

Do not hide the whole category

A tempting fix is error_reporting(E_ALL & ~E_DEPRECATED). It removes the noise, and it also removes every other deprecation, including others on the same manual page, such as implicit float-to-int conversions that lose precision, which affect array keys and operators. If you must mute them in production while you work, keep reporting on in staging and keep a dated list of what remains.

After the fix

Run the pages that read the affected values with complete and empty input, since the empty case raised the message. Our guide to adding tests to legacy PHP code shows how to keep that coverage. We have not run these changes on your code, so check them on a copy.

If the log is dominated by plugins or an old framework, the lasting answer is usually an update or a planned modernisation. Our legacy system support work covers both, and our modernisation roadmap shows the order we follow. Related errors: undefined variable and array key warnings and the count() TypeError in PHP 8.

Frequently asked questions

Will passing null to a built-in function stop working?

In PHP 8.1 it still works and emits a deprecation, according to the manual. Fix it now, because deprecated behaviour can change in later versions.

Does this affect PHP 8.0?

No. The message begins in PHP 8.1, so you will not see it on PHP 8.0 or 7.4.

Can I silence it for a plugin I do not control?

You can lower reporting as a stopgap, but the lasting fix is to update or patch the plugin. Check whether a newer release supports your PHP version.

Thousands of deprecation lines a day?

We clean up the patterns behind them, test the result and move your platform to a supported PHP version.

See our Legacy System Support

Read about our legacy PHP modernisation services →

Ravi Raghav, Founder & CEO of AllScript Lab
Written by Ravi Raghav Founder & CEO