Every single PHP programmer alive makes the same mistake.
The mistake is posted all over the internet like it is correct but it's not.
The mistake shows a lack of understanding of variable types returned from functions
as well as what a boolean value is.
I'm not kidding.
One day the data your script is looking for to determine what to do will be in the first byte position of the data.
Then all your php will blow up.
You are getting away with it for now, or at least you think.
Do you know what data you missed?
So, here is the incorrect code used by every PHP programmer alive :
if (stripos($string,'hello')) { ....
See php script below for the correct code.
Now if someone really understands "if statements" and the stripos command then they should immediately know this is wrong.
Why?
Because "if statements" only do boolean test and stripos will return non-boolean values as well as boolean false.
It says that in the manual :
http://us2.php.net/manual/en/function.stripos.php
Read boolean values conversion :
http://us2.php.net/manual/en/language.types.boolean.php
0 evaluates as false in an if statement; 0 is the first position of a string however.
Meaning you get "false" when the string is there and starts in position 0.
Don't believe me?
Ok, then run this code :
<?
$string = "hello";
// wrong code
if (stripos($string,'hello')) {
echo "True";
}
else {
echo "false";
}
echo "<br><br>";
// correct code
if (stripos($string,'hello') !== false ) {
echo "True";
}
else {
echo "false";
}
?>
Notice the "!
== false" and NOT "!
=false"
Don't believe what I said about "if statements"?
Then run these :
<?
if ("1") {
echo "true";
}
else {
echo "false";
}
?>
<?
if ("-1") {
echo "true";
}
else {
echo "false";
}
?>
<?
if ("0") {
echo "true";
}
else {
echo "false";
}
?>
Happy PHP blow up day!
