function check($text){
if(strpos('a', $text) == FALSE && strpos('b', $text) == FALSE){
echo 'error';
} else {
echo 'ok';
}
}
echo check('text') . "\n";
echo check('asd') . "\n";
echo check('bnm') . "\n";
echo check('test') . "\n";
echo check('abc') . "\n";
直播:http://codepad.org/W025YYuH
为什么这不起作用?本次回归:
1 错误2 错误3 错误4 错误5 错误
但应该是:
1 错误 2 正常 3 正常 4 错误 5 正常
反转参数的位置,第一个参数是字符串,第二个参数是你要在字符串中搜索的内容。
strpos ( 'The string to search in' ,'the argument of search' )
那么 == 将无法按预期工作 因为 'a' 的位置是第 0 个(第一个)字符。
试试这个:
function check($text){
if(strpos($text, 'a') === FALSE && strpos($text, 'b') === FALSE){
echo 'error';
} else {
echo 'ok';
}
}
echo check('text') . "\n";
echo check('asd') . "\n";
echo check('bnm') . "\n";
echo check('test') . "\n";
echo check('abc') . "\n";
你的参数搞错了,改成:
if(strpos($text, 'a') === FALSE && strpos($text, 'b') === FALSE){
另请注意,您需要使用相同的运算符 (===) 检查布尔值
false