phpstringstripos

Why does a string in PHP not contain an empty string?


Consider the following piece of code which somehow returns false:

<?php var_dump(stripos("foo", "")); ?>

It returns bool(false), not int(0) on PHP 7.4.

I thought every string contained the empty string.

So what is the cause of this behavior?


Solution

  • From documentation:

    Returns false if the needle was not found.

    The purpose of this is possibility to strictly compare result with false in order to find out that needle is not found in haystack.

    if (stripos("foo", "") === false) {
        // Here we definitely know "" is not contained in "foo"
    }