drr47973
2017-04-13 07:43
浏览 105
已采纳

当我在条件中使用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.

图片转代码服务由CSDN问答提供 功能建议

当我使用此代码时,它显示正确的结果

 &lt;?php 
 $ string ='有一些文字'; 
 $ find ='some'; 
函数iscontain($ string,$ find){
 $ check = strpos($ string,$ find)  ); 
 if($ check === false){return 0;  } else {return 1;  } 
} 
 
 if(iscontain($ string,$ find)){
 echo“true”;  
}其他{
 echo“false”;  
} 
 
 //输出:true 
 
?&gt; 
   
 
 

但是当我将false更改为true并更改返回值时,如果 check为true所以返回1 else 0所以逻辑上是正确的但是结果没有显示正确

 &lt;?php 
 $ string ='有一些文字'; 
 $  find ='some'; 
 function iscontain($ string,$ find){
 $ check = strpos($ string,$ find); 
 if($ check === true){return 1;  } else {return 0;  } 
} 
 
 if(iscontain($ string,$ find)){
 echo“true”;  
}其他{
 echo“false”;  
} 
 
 //输出:false 
 
?&gt; 
   
 
 

为什么两个结果都不同..? 谢谢。

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • doukezi4606 2017-04-13 07: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.

    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题