phpstrpos

strpos() === true always returns false


I get a project and i see a piece of code as follows:

$orderby = $_REQUEST['orderby'];
if (strpos($orderby, 'd') === true) {
    echo "exists";
} else {
    echo "not ";
}

In any case, I input 'd' or others parameters the page always returning 'not'. So, how to input correct parameters to make the page return 'exists'?


Solution

  • strpos() can never return TRUE. If the string is found it returns the position. If the string is not found it returns FALSE. So you should compare with FALSE, not TRUE.

    if (strpos($orderby, 'd') === false) {
        echo "not exists";
    } else {
        echo "exists";
    }