PHP warns that null is passed to a built-in function
Deprecated: Passing null to parameter #1 ($string) of type string is deprecatedShort 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.
Fix it fast
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"2Match what you see, then jump to its fix
- 1
The 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
The argument is $_POST['x'], $row['name'] or a config value that is sometimes absent.An array key or setting may be missingFix - 3
The file path in the warning is inside vendor/ and your own code is correct.The notice comes from vendor code you cannot editFix
- 1
What does the error look like?
Deprecated: Passing null to parameter #1 ($string) of type string is deprecatedDeprecated: 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 warning2Fix
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 | headConfirmed by php.net
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.phpConfirmed by php.net
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_DEPRECATED3Verify
Reload PHP and check that the notice no longer fills the log.
php -r "echo error_reporting(), PHP_EOL;"Confirmed by php.net
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
Replacing strftime() and gmstrftime() in PHP 8.1 and later
Deprecated: Function gmstrftime() is deprecated
strftime() and gmstrftime() are deprecated since PHP 8.1.
3 causes, each with a fixBack end
Android gets HTML instead of JSON from a PHP API
org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject
The response body is not JSON.
2 causes, each with a fixBack 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 fixBack end
Apache returns 500 with Premature end of script headers
500 Internal Server Error
Apache ran your script, but the script printed no valid HTTP header before its output.
3 causes, each with a fixBack end
Apache returns 503 or 504 because PHP-FPM did not answer in time
Error dispatching request
Apache sent the request to PHP-FPM and gave up waiting.
3 causes, each with a fixBack end
AWS SDK for PHP cannot get credentials from the instance profile metadata server
Error retrieving credentials from the instance profile metadata server.
The SDK tries the EC2 instance metadata service when it has no valid credentials from anywhere earlier in its chain.
4 causes, each with a fix