Article

Passing Null to Non-Nullable Parameters (PHP 8.1)

Clear PHP 8.1 deprecations caused by passing null into internal functions that declare non-nullable parameters.

PHP 8.1 deprecated passing null to internal function parameters that are not nullable. The values often came from array keys, database columns, or loosely typed helpers. Official migration notes under migration81 document the broader 8.1 hardening; this page focuses on the null-parameter pattern.

# LEGACY — $name may be null from request/db
strlen($name);
str_contains($haystack, $needle); // when $needle is null

# MODERN — make the contract explicit
strlen($name ?? '');
str_contains($haystack, $needle ?? '');

# Better: reject illegal state early
if ($name === null) {
    throw new InvalidArgumentException('name required');
}
strlen($name);

How to find call sites

Do not blanket-cast everything to string—some APIs should fail closed when null appears. Parent: deprecated hub. Related step: 8.0→8.1.

Related scanners: PHP Version Compatibility Checker · PHP Deprecated Checker.

Related tools

Related reading