简体   繁体   English

当我在条件下使用true时,strpos函数无法正常工作?

[英]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 但是当我将false更改为true时,还更改了返回值,例如check是否为true,因此返回1 else 0,因此在逻辑上是正确的,但结果未显示正确

    <?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. 谢谢。

This happens because strpos never returns true . 发生这种情况是因为strpos 永远不会返回true

It can return false or integer number. 它可以返回false或整数。

So in your second function else branch always run returning 0 which is considered falsy. 因此,在您的第二个函数中, else分支始终运行,返回0 ,这被认为是虚假的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM