note 128207 added to function.strpos
| From: | JulianEgelstaff at main dot php dot net | Date: | Fri, 10 Feb 2023 15:46:24 +0000 |
| Subject: | note 128207 added to function.strpos | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-220163@lists.php.net to get a copy of this message | ||
Lookout for logic inversion in old code!
In PHP 8, if the needle is an empty string, this function will return 0 (not false), implying the
first character of the string matches the needle. Before PHP 8, it would return false when the
needle is an empty string.
There other string functions that are affected by similar issues in PHP 8: strpos(), strrpos(),
stripos(), strripos(), strchr(), strrchr(), stristr(), and this function, strstr()
If you are checking if the return value === false then you will be misled by this new behaviour. You
also need to check if the needle was an empty string. Basically, something like this:
<?php
$result = $needle ? strstr($haystack, $needle) : false;
?>
----
Server IP: 45.112.84.4
Probable Submitter: 80.90.13.130 (proxied: 142.114.226.212)
----
Manual Page -- https://php.net/manual/en/function.strpos.php
Edit -- https://main.php.net/note/edit/128207
Del: integrated -- https://main.php.net/note/delete/128207/integrated
Del: useless -- https://main.php.net/note/delete/128207/useless
Del: bad code -- https://main.php.net/note/delete/128207/bad+code
Del: spam -- https://main.php.net/note/delete/128207/spam
Del: non-english -- https://main.php.net/note/delete/128207/non-english
Del: in docs -- https://main.php.net/note/delete/128207/in+docs
Del: other reasons-- https://main.php.net/note/delete/128207
Reject -- https://main.php.net/note/reject/128207
Search -- https://main.php.net/manage/user-notes.php