Reputation: 193
I have this issue with a string:
$val = 'NOT NULL';
if(stripos($val, 'NULL') !== FALSE){
echo "IS $val";
}
It evaluates fine, but if I use === TRUE
as evaluator, things go wrong.
The answer eludes me, please help me understand.
Upvotes: 4
Views: 604
Reputation: 72991
If you read the documentation for stripos()
you'll find.
Returns the position of where the needle exists relative to the beginnning of the haystack string (independent of offset). Also note that string positions start at 0, and not 1.
Returns
FALSE
if the needle was not found.
It does not return TRUE
. Since you are using strict equality, your condition will never be true.
If you did stripos($val, 'NULL') == TRUE
then your code would execute if NULL
were found at position 0
- since PHP will do some type-juggling and effectively 0 == (int)true
.
The appropriate way to test for existence using stripos()
is what you have:
if (stripos($val, 'NULL') !== FALSE){
echo "IS $val";
}
Upvotes: 8
Reputation: 37365
Since ===
and !==
are strict comparison operators - !== false
is not the same as ===true
since, for example, 1!==false
is ok (values and types are not equal), but 1===true
is not ok (values are equal, but types are not).
This sample indicates the meaning of strict comparison - i.e. not only values matters, but also types of compared data.
Upvotes: 1
Reputation: 24661
The answer is because you are using the strict equality operator. The function itself returns an int (or boolean if the needle is not found). The return value is not equal (in the strict sense, both value and type) to true, which is why the check fails.
Upvotes: 1