Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

PHP warns that null is passed to a built-in function

ErrorDeprecated: Passing null to parameter #1 ($string) of type string is deprecated

Short answer

Since PHP 8.1, built-in functions no longer accept null for a non-nullable scalar parameter silently. The warning names the function and the parameter number. Find where that value can be null, then give it a default with ?? or cast it, instead of hiding the notice.

  • Applies to PHP 8.3, PHP 8.4
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Make the notice show a file and line for every call, so you fix the cause and not the symptom.

    php -d error_reporting=E_ALL -d display_errors=1 public/index.php 2>&1 | grep "Passing null"
  2. 2Match what you see, then jump to its fix

    1. 1The message names a function like trim(), strlen() or str_contains(), and the argument is a value from a form, a database column or an optional setting.A variable that can be null reaches a string functionFix
    2. 2The argument is $_POST['x'], $row['name'] or a config value that is sometimes absent.An array key or setting may be missingFix
    3. 3The file path in the warning is inside vendor/ and your own code is correct.The notice comes from vendor code you cannot editFix

What does the error look like?

  • Deprecated: Passing null to parameter #1 ($string) of type string is deprecated
  • Deprecated: Passing null to parameter #2 ($needle) of type string is deprecated

Cause 1 of 3

A variable that can be null reaches a string function

1Check

The manual says scalar types of built-in functions used to be nullable by default, and that this is deprecated to match user-defined functions. Find the variable on the reported line and see where it can become null.

<?php
var_dump($value); // on the line named in the warning

2Fix

If an empty value is right, convert it on purpose. The manual states that null converts to an empty string.

$clean = trim((string) $value);

3Verify

Run the page again. The notice for that line is gone and the output is unchanged.

php -d error_reporting=E_ALL -d display_errors=1 public/index.php | head

Confirmed by php.net

Did this fix it?

Cause 2 of 3

An array key or setting may be missing

1Check

The null coalescing operator returns the right side when the left side is null or missing, and it raises no warning for a missing array key.

<?php
var_dump(isset($_POST['name']), $_POST['name'] ?? null);

2Fix

Give the value a default where you read it, before it goes to the function.

$name = trim($_POST['name'] ?? '');

3Verify

Submit the form once with the field empty and once without it.

curl -s -X POST -d "other=1" http://localhost:8000/form.php

Confirmed by php.net

Did this fix it?

Cause 3 of 3

The notice comes from vendor code you cannot edit

1Check

Deprecations are reported at level E_DEPRECATED. Your error_reporting setting decides whether they are shown or logged.

php -r "echo error_reporting(), PHP_EOL;"

2Fix

Update the package first. Until a fixed release exists, keep reporting everything except deprecations in production, and keep them on in development.

composer update vendor/package
# php.ini on the production server only
error_reporting = E_ALL & ~E_DEPRECATED

3Verify

Reload PHP and check that the notice no longer fills the log.

php -r "echo error_reporting(), PHP_EOL;"

Confirmed by php.net

Did this fix it?

How do I stop it coming back?

  • Run your test suite with error_reporting set to E_ALL so new deprecations fail in CI, not in production.
  • Type your own function parameters and return values, so null cannot travel silently.
  • Read request input through one helper that applies defaults.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Back end

Apache returns 403 when a URL contains %3F

a request like /page%3Fname=test is refused while /page?name=test works

Recent Apache httpd blocks a rewrite when the request contains an encoded question mark (%3F) and the rewritten target has a literal ?.

2 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.