drr47973 2017-04-12 23:43
浏览 116
已采纳

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

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.

展开全部

  • 写回答

1条回答 默认 最新

  • doukezi4606 2017-04-12 23:46
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部