PHP 8.1 – Deprecation when you pass NULL to non-null param

PHP 8.1 – Deprecation when you pass NULL to non-null param

missing tooth

So you have some random null values passed over to internal PHP function for non-null params? Well you are in trouble in PHP 8.1 and even more trouble when PHP 9 would pop in.

This change synchronizes the behavior of internal functions, by throwing a deprecation warning in PHP 8.1, which will become a TypeError in the next major version.

var_dump(str_contains("foobar", null));
// Deprecated: Passing null to argument of type string is deprecated

If

  • null is passed to an argument of an internal function,
  • the argument is not nullable,
  • the argument accepts at least one scalar type (one of bool, int, float, or string),
  • coercive typing mode is used, i.e. strict_types=1 is not enabled,

then

  • in PHP < 8.1 the null value will be silently coerced to either false00.0 or “”.
  • in PHP >= 8.1 a deprecation error is thrown, but the value is still coerced and the call still takes place.
  • in PHP >= 9.0 a TypeError is thrown, consistent with the behavior of user-defined functions.

More about PHP 8.1

Read about all PHP 8.1 features and changes in here.

Leave a Comment