[php-src] Issue #8532: Allow nullable haystack in string-functions
| From: | Wuschba | Date: | Wed, 11 May 2022 09:53:47 +0000 |
| Subject: | [php-src] Issue #8532: Allow nullable haystack in string-functions | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-241457@lists.php.net to get a copy of this message | ||
Issue: https://github.com/php/php-src/issues/8532
Author: Wuschba
### Description
Hello,
since PHP 8.1. this throws an E_DEPRECATED:
```
$haystack= null;
if (str_contains($haystack, "needle")) {
...
}
```
I understand that the correct usage of types is important for a clean programming, but whats the
reason that $haystack could not be nullable in string-functions?
For example: We fetch a lot values from the database in our workflows, and the text-values there can
be null as well of course, so in PHP 7.x it was a straight approach just to use:
```
$haystack = getTxtDatabaseValue("txtfield"); // returns ?string
if (strpos($haystack, "needle") === false) {
...
}
```
just meaning: If $haystack is null, "needle" can not be inside $haystack, so strpos(null,
"needle") always returns false.
Of course we can do something like:
```
$haystack = getDatabaseValue(); // returns ?string
if ($haystack !== null && str_contains($haystack, "needle")) {
...
}
```
but the extra check is somehow inconvenient, and of course this breaks a lot of our existing code,
which makes the update of the PHP version very expensive. So it would be great if you could consider
to allow a nullable string here or perhaps explain to me why it doesn't make sense to allow
nullables in string-functions.