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.
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";
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With