Search code examples
phpstripos

stripos(): Non-string needles warning when passing string characters


After upgrading my servers to PHP 7.3 I began seeing warnings such as:

PHP Deprecated: stripos(): Non-string needles will be interpreted as strings in the future. Use an explicit chr() call to preserve the current behavior...

in reference to the following code: if (stripos($checkforcomma, "," !== 0))

Why is it seeing the comma as something other than a string character? Following the suggestion in the error message, chr(44) would output a comma and in fact generates the exact same error message when I swap "," for chr(44).


Solution

  • As Jeto commented on your question, the fix for this is to correct the typo to

    if (stripos($checkforcomma, ",") !== 0)
    

    I think it's worth pointing out that fixing this typo to stop the deprecation notice will change how your program works. With the current code, it wasn't doing what it was intended to do, even though there was no warning.

    You're effectively checking for chr(1) in the string instead of a comma, because "," != 0 evaluates to true, which will get converted to an int 1 to use in stripos.

    Unless you sometimes had a chr(1) in the middle of some of the $checkforcomma strings then anything that was supposed to happen inside that if was never happening, so after you fix the typo your program will be more likely to execute that code that probably never ran before. Assuming everything looked like it was working properly before this, that might cause other problems.

    You're probably already aware of this possibility, but I thought it could be helpful to add a little warning for someone with a similar problem who might come across this in the future.