It returns bool(false), not int(0) on PHP 7.4" /> It returns bool(false), not int(0) on PHP 7.4" /> It returns bool(false), not int(0) on PHP 7.4"/>

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

85 Views Asked by At

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?

1

There are 1 best solutions below

2
Yevgeniy Kosmak On BEST ANSWER

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"
}