PHP strpos() not working as expected? It could be a “non-strict” comparison problem.
When using strpos() to determine whether a substring exists within a string the results can be misleading: Remember FALSE == 0?
Consider the following:
$quote = 'Dave rocks';
if (strpos($quote, 'Dave')) {
echo 'Dave is awesome.';
} else {
echo 'Dave is not awesome.';
}
strpos() Returns position is 0 ( zero ) that is evaluated as FALSE so, “Dave is not awesome”.
Much better. In this case adding the strict comparison === ( 3 equals ) to the “if” statement asks if strpos() returns a number and is not strictly FALSE. So, “Dave is awesome”
$quote = 'Dave rocks';
if (strpos($quote, 'Dave') !== FALSE) {
echo 'Dave is awesome.';
} else {
echo 'Dave is not awesome.';
}