Evaluate stripos (), what is the difference between: == FALSE and === TRUE?

I have this problem with the line:

$val = 'NOT NULL'; if(stripos($val, 'NULL') !== FALSE){ echo "IS $val"; } 

He evaluates the penalty, but if I use === TRUE as an appraiser, everything goes wrong. The answer eludes me, please help me understand.

+4
source share
3 answers

If you read the documentation for stripos() , you will find.

Returns the position of the needle relative to the beginning of the haystack line (regardless of offset). Also note that line positions start at 0, not 1.

Returns FALSE if the needle is 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 was found at position 0 - since PHP would do type manipulation and effectively 0 == (int)true .

The appropriate way to check for existence with stripos() is what you have:

 if (stripos($val, 'NULL') !== FALSE){ echo "IS $val"; } 
+7
source

The answer is that you are using the strict equality operator. The function itself returns int (or boolean if the needle is not found). The return value is not equal (in the strict sense of both value and type) to true, therefore, the check is not performed.

+1
source

Since === and !== strict comparison operators - !== false does not match ===true , because, for example, 1!==false is ok (values ​​and types are not equal), but 1===true not suitable (values ​​are equal, but no types).

This example specifies the value of strict comparison - i.e. Not only the values ​​are important, but also the types of data being compared.

+1
source

Source: https://habr.com/ru/post/1498392/


All Articles