2017-04-13 90 views
0

当我使用这个代码就告诉我正确的结果当我在条件中使用true时,strpos函数无法正常工作?

<?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 

?> 

但是,当我改变假成真,改变返回值一样,如果检查是true,所以返回1,否则为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 

?> 

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

+1

请阅读[strpos()']的文档(http://php.net/manual/en/function.strpos.php)。如果找到它,则返回字符串的_position_;如果未找到,则返回“false”。它实际返回“true”时没有状态。 –

+0

[简单的PHP strpos函数不工作,为什么?]可能的重复(https://stackoverflow.com/questions/4858927/simple-php-strpos-function-not-working-why) – mickmackusa

回答

1

这是因为strpos从未回报true。可以返回false或整数。

因此,在您的第二个功能else分支始终运行返回0这被认为是虚假的。