Since I have neglected my blog this week I decided to write a small continuation of the discussion on Types in PHP.
I was helping a fellow coder yesterday, and he was having problems with a simple comparison statement. The situation he found himself in was this:
A $_POST value could either be 0 (zero) or a text string. If incoming value was 0 (zero) that meant a new record needed to be inserted in the database, if it was a text string then the record already existed in the database and needed to be updated. Consequently he wrote his comparison statement like this:
<?php
if ($_POST["id"] == 0) {
//insert db record
}
else {
//update db record
}
?>
Everything looked fine to him, but no matter what value was passed in the $_POST variable the comparison statement evaluated to TRUE, meaning a new record was inserted into the database.
If you want to check it out yourself run this code:
<?php
$_POST["id"] = "0";
if ($_POST["id"] == 0) {
echo "insert db record / ";
}
else {
echo "update db record / ";
}
$_POST["id"] = "string of text";
if ($_POST["id"] == 0) {
echo "insert db record";
}
else {
echo "update db record";
}
?>
If you run the code above it will print: ‘insert db record / insert db record’.
Now this is a bad comparision to begin with, since all values coming from $_POST are a string of text; but that was not the problem he was experiencing.
Why does “string of text” equal zero in the second comparision statement?…. because of TYPES, of course.
In the both comparison statements we are comparing a string and an integer, since we did not use the Identical comparison operator (===), PHP converts the string to an integer before making the comparison. This means it is the same as casting the string as an integer then making the comparison; such as:
<?php
$_POST["id"] = "string of text";
$_POST["id"] = (int)$_POST["id"];
if ($_POST["id"] == 0) {
echo "insert db record";
}
else {
echo "update db record";
}
?>
When casting “string of text” as an integer “string of text” becomes 0 (zero). Check it out:
<?php $_POST["id"] = "string of text"; $_POST["id"] = (int)$_POST["id"]; var_dump($_POST["id"]); ?>
The above code will print: “int(0)”.
So how do we get this comparison to evaluate as intended? We cannot use the Identical (===) comparison operator, because as I said before, everything coming from $_POST is a string. This means we would have the same problem in reverse. The comparison would always evaluate to FALSE, and it would attempt to update records that do not exist. What we need is for PHP to evaluate both operands as strings, this can be accomplished two ways. Either by placing quotes around the 0 (zero) making it a string, or casting the 0 (zero) as a string. Such as:
<?php
if ($_POST["id"] == "0") {
//insert db record
}
else {
//update db record
}
if ($_POST["id"] == (string)0) {
//insert db record
}
else {
//update db record
}
?>
Personally I think the first example is more appropriate, if you want to compare 0 (zero) as a string just write it as a string. I simply provided the casting example because we have previously discussed casting.
If you would like to know more about Types in PHP you can read my previous post: Variable Types – Why Care? and follow the links at the bottom of the article.



PDF













