Passing null to Non-nullable Parameters of Built-in Functions is Deprecated in PHP 8.1

Passing null to Non-nullable Parameters of Built-in Functions is Deprecated in PHP 8.1

PHP allows specifying types for function and class method parameters. Passing null to a non-nullable parameter will emit a fatal error. In versions prior to PHP 8.1, this behavior only applies for user-defined functions and class methods, and not for built-in functions.

For example, strlen is a built-in PHP function which accepts string type parameter. In versions prior to PHP 8.1, passing null to the strlen function will return 0:

<?php

echo strlen(null); // 0

Since PHP 8.1, passing null to non-nullable parameters of built-in functions is deprecated. For example, the following code outputs 0 and emits a deprecation notice:

<?php

echo strlen(null); // 0 and deprecation notice

Output:

Deprecated: strlen(): Passing null to parameter #1 ($string) of type string is deprecated in ...
0

Leave a Comment

Cancel reply

Your email address will not be published.