Ehsan Ilahi
Ehsan Ilahi

Reputation: 298

strpos function is not working correctly when i use true in condition why?

When i am using this code it show me correct result

    <?php
        $string = 'there is some text';
        $find = 'some';
        function iscontain($string,$find){
           $check = strpos($string, $find);
           if ($check === false) { return 0; }else{ return 1; }
        }

        if(iscontain($string,$find)){ 
            echo "true"; 
        }else{
            echo "false"; 
             }

     //output:  true

?>

But when i change false into true and also change return values like if check is true so return 1 else 0 so logically is correct but result not showing correct

    <?php
    $string = 'there is some text';
    $find = 'some';
    function iscontain($string,$find){
       $check = strpos($string, $find);
       if ($check === true) { return 1; }else{ return 0; }
    }

    if(iscontain($string,$find)){ 
        echo "true"; 
    }else{
        echo "false"; 
    }

 //output : false

?>

Why both results are different ..? Thanks.

Upvotes: 0

Views: 73

Answers (1)

u_mulder
u_mulder

Reputation: 54796

This happens because strpos never returns true.

It can return false or integer number.

So in your second function else branch always run returning 0 which is considered falsy.

Upvotes: 1

Related Questions