2017-04-13 3 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

strpostrueを返します。

falseまたは整数を返すことができます。

したがって、第2の機能では、elseブランチが常に実行され、0が返され、これは偽と見なされます。

関連する問題