当我在条件中使用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.
当我使用此代码时,它显示正确的结果 p>
&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;
code> pre>
但是当我将false更改为true并更改返回值时,如果 check为true所以返回1 else 0所以逻辑上是正确的但是结果没有显示正确 p>
&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;
code> pre>
为什么两个结果都不同..? 谢谢。 p>
div>
答
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.