PHP, like most programming languages evaluates the conditional expression (the code inside an if
statement) to a boolean expression:
if (123)
echo '123 is true';
This will echo the text because, when it comes to ints, everything except 0 is true (or truthy as it is also known).
You can check the official documentation here to get a detailed overview of what types/values evaluate to true and which to false.
Of course, in the case of strstr
and stristr
, when no substring is found, the function will actually return false
, and so a type and value check of the return value is not uncommon:
if (stristr($string, 'needle') !== false)
echo 'The needle was found';