0

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.

Ehsan Ilahi
  • 298
  • 5
  • 15
  • 1
    Please read the [documentation for `strpos()`](http://php.net/manual/en/function.strpos.php). It returns the _position_ of the string if it's found and `false` if it's not found. It doesn't have a state when it actually returns `true`. – M. Eriksson Apr 13 '17 at 07:46
  • Possible duplicate of [Simple PHP strpos function not working, why?](https://stackoverflow.com/questions/4858927/simple-php-strpos-function-not-working-why) – mickmackusa Aug 31 '17 at 05:19

1 Answers1

1

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.

u_mulder
  • 54,101
  • 5
  • 48
  • 64