<?php
if(stripos('http://cp.uctorrent.com', 'cp.utorrent.com') >= 0){
echo "Good1";
}else{
echo "Bad1";
}
if(stripos('http://uctorrent.com', 'cp.utorrent.com') >= 0){
echo "Good2";
}else{
echo "Bad2";
}
?>
output is
Good1Good2
whereas it should be
Good1Bad2
utorrent <---> uctorrent
i am such an idiot...
it was spell mistake ...
comparing uctorrent with utorrent
sorry every one
<?php
if(false >= 0) echo "Good";
else echo "Bad";
// this code prints Good
?>
It's not a bug, it's a "weird" boolean conversion.
stripos
returns false when the string is not found, and false converts to 0 in PHP.
Directly from the documentation (the problem is the other way around) :
Warning This function may return Boolean FALSE, but may also return a non-Boolean value which evaluates to FALSE, such as 0 or "". Please read the section on Booleans for more information. Use the === operator for testing the return value of this function.
In case 2 stripos
returns false
as the search fails and false
when compared with 0
returns true
.
The right way of doing it is to use the identity operator which checks both type and value:
if(stripos('http://cp.uctorrent.com','cp.utorrent.com') !== false)
echo "Good1"; ^^^^^^^^^^
else
echo "Bad1";
If needle is not found, stripos() will return boolean FALSE.
http://php.net/manual/en/function.stripos.php
Boolean FALSE in PHP is equivalent to integer 0, which is >= 0
.
If you want to check whether stripos failed to get a match, you need to test for type and value with !== or ===, for example:
<?php
if(stripos('http://cp.uctorrent.com','cp.utorrent.com')!==false)echo "Good1";
else echo "Bad1";
if(stripos('http://uctorrent.com','cp.utorrent.com')!==false)echo "Good2";
else echo "Bad2";
?>
Reading the manual would help greatly:
Warning
This function may return Boolean FALSE, but may also return a non-Boolean value which evaluates to FALSE, such as 0 or "". Please read the section on Booleans for more information. Use the === operator for testing the return value of this function.
Again, try this and avoid the issues caused by microoptimization function use patterns:
if (stristr('http://cp.uctorrent.com', 'cp.utorrent.com')) {
echo "Good1";
}
else {
echo "Bad1";
}
Below $p has value 'false' (means 0), so it's >=0
$p = stripos('http://uctorrent.com','cp.utorrent.com');
You need to check stripos('http://uctorrent.com','cp.utorrent.com') !== false first then get $p (found position) like above...