preg_replace(string, null) is allowed?

php.internals

David Rodrigues

5 years ago
Hello! I am in doubt if passing null as the second argument to preg_replace() is valid. Actually, it works as expected. https://3v4l.org/0TITS But doc only accepts array|string. I am asking for it because PhpStorm will not accept it. So if it is valid, I will do a PR to PS stubs. Thanks! Atenciosamente, David Rodrigues

Nikita Popov

5 years ago
On Wed, Feb 17, 2021 at 9:28 PM David Rodrigues <david.proweb@gmail.com> wrote:
> Hello! > > I am in doubt if passing null as the second argument to preg_replace() is > valid. Actually, it works as expected. > > https://3v4l.org/0TITS > > But doc only accepts array|string. > > I am asking for it because PhpStorm will not accept it. So if it is valid, > I will do a PR to PS stubs. >
On PHP 8.1.0-dev your example prints: Deprecated: preg_replace(): Passing null to parameter #2 ($replacement) of type array|string is deprecated in %s on line 3 Under strict_types=1 it already throws. So no, it is not valid. Regards, Nikita

David Rodrigues

5 years ago
Thanks! There is a reason why it is not supported anymore? There is a difference in output or performance, maybe? Atenciosamente, David Rodrigues Em qua., 17 de fev. de 2021 às 17:37, Nikita Popov <nikita.ppv@gmail.com> escreveu:

Guilliam Xavier

5 years ago
On Thu, Feb 18, 2021 at 3:15 AM David Rodrigues <david.proweb@gmail.com> wrote:
> Thanks! > > There is a reason why it is not supported anymore? >
https://wiki.php.net/rfc/deprecate_null_to_scalar_internal_arg "PHP RFC: Deprecate passing null to non-nullable arguments of internal functions". In short: to fix an historical inconsistency between internal and user-defined (esp. polyfilled) functions. First discussed at https://externals.io/message/105845 "Handling of null arguments to internal functions". Rebooted as https://externals.io/message/112327 (and https://externals.io/message/112996 ) for the RFC.
-- Guilliam Xavier