Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

evaluate stripos(), what is the difference between !== FALSE and === TRUE?

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.

like image 238
derei Avatar asked Dec 26 '22 22:12

derei


2 Answers

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";
} 
like image 183
Jason McCreary Avatar answered Jan 13 '23 12:01

Jason McCreary


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.

like image 33
Jeff Lambert Avatar answered Jan 13 '23 12:01

Jeff Lambert