This looks so simple but obviously I'm doing something very wrong. This demonstrates the problem.
<?PHP
$search="a";
$total = "aNbh jK ";
if( stripos($total,$search,0) == TRUE) {
echo"found";
} else {
echo "Not found";
}
?>

$search is not found! If $search == "n" it does work.
What's wrong please?
Crusty

    The problem is that if stripos returns a non-zero value, it will be converted to true, but if it returns a zero, it will be converted to false. Since 'a' is the first character, stripos will thus return 0, and so the comparison with true evaluates to false, thus "Not found" is printed.

    The solution is to compare, using operator=== (or !==), with false, since stripos returns false if the search string is not found:

    <?php
    $search="a";
    $total = "aNbh jK ";
    if (stripos($total, $search, 0) !== false) {
        echo "found";
    } else {
        echo "Not found";
    }

    By the way, please do not cross post.

      Write a Reply...