phpstripos

why does stripos evaluates to false


I have a string $x = 'hello world.'. I want to see if $x contains string $y = 'hello'.

If i do

stripos($x, $y); 

it returns a numeric 0, the index where string $y starts. However this evaluates to FALSE. How can I evaluate this so it returns true?


Solution

  • Read the warning notice from manual:

    Warning: This function may return Boolean FALSE, but may also return a non-Boolean value which evaluates to FALSE. Please read the section on Booleans for more information. Use the === operator for testing the return value of this function.

    So you should use like this:

    if(stripos($x, $y) !== FALSE)
    // Found
    else
    // Not found
    

    For reference see this example from manual.