We have all heard PHP is a loosely typed language. If you are unsure of the meaning of the term “loosely typed” the definition in the PHP manual states (at least for our discussion):
PHP – Manual
The type of a variable is usually not set by the programmer; rather, it is decided at runtime by PHP depending on the context in which that variable is used.
In code terms this means that we do not have to define a “type” for each variable, we can simply give the variable a value and the PHP engine will determine (through context clues) what the variable type should be at that instant (runtime). Also a variable’s type may change any number of times during the execution of a script, as shown by this example:
<?php //the variables are defined as strings //(with quotes) $integer1 = "50"; $integer2 = "14"; //we can use them //in mathematical equations echo $integer1 + $integer2; //64 //we can also treat them //as strings and concatenate them echo $integer1 . $integer2; //5014 //if we want to get fancy we can also use //them as arrays, which actually comes in //handy once in a while echo $integer1[0] + $integer2[0]; // 6 //**note if the variables were defined as //integers this last example would not //work** ?>
Given this information, most PHP developers (especially those who use PHP exclusively) simply forget about variable types and PHP does all the work for them. I was one of those developers until I read an article (unable to locate the link at the moment) about optimizing your PHP code.
In the article the benchmarks showed a significant improvement in processing time when using the comparison operators Identical (===) and Not Identical (!==) as opposed to Equal (==) and Not Equal (!=).
**Please note I have no idea if this optimization still true, or ever was, but for our purposes it really does not matter.**
The difference between (Not) Identical and (Not) Equal is that PHP checks both the value and type of the two operands, meaning:
<?php
//integer1 is defined as a string
$integer1 = "50";
//integer1 is defined as an integer
$integer2 = 50;
if ($integer1 == $integer2) {
echo "Equal / ";
}
else {
echo "NOT Equal / ";
}
if ($integer1 === $integer2) {
echo "Identical";
}
else {
echo "NOT Identical";
}
?>
The above code will print “Equal / NOT Identical”. During the first comparison (equal), type is not taken into consideration so the string “50″ is equal to the integer 50; but in the second comparison (identical), a string and an integer are not the same, hence not identical.
Obviously when you start comparing two values using the (Not) Identical operator, type becomes very important and a little challenging at first. You will inevitably forget to take into consideration variable types, and some code will simply not execute as you planned.
OK, so now that I have briefly explained types (string and integer) in PHP terms, the question remains, why do we care? Simply put: because it is part of PHP. If you just want to create scripts which randomly display quotes, email the contents of a form, or other small tasks then there really is no reason to get involved with types or type casting; but since you are reading this post (and have made it this far) I will assume you are slightly more interested in creating larger projects and applications. If this is the case then you need to learn about all aspects of PHP and in tern you will then be able to write more complicated, efficient and stable code.
As a quick example: you should know that all data coming from $_POST and $_GET arrives in PHP as stings (actually an array of strings; kind of like field values from MySQL
). So if you would like to check that an incoming value is numeric, you could use the function is_numeric() or ctype_digit(). Of course if you wanted to determine if a value is numeric one of the quickest and most efficient ways is to cast the value.
Since 99% of the time 0 (zero) is not a valid value for incoming numeric data you can simply cast the variable. If the value only contains non-numeric characters; when cast as an integer PHP will set the value to 0 (zero). If the value is entirely numeric or begins with a number the numeric portion is converted. For example:
<?php
$id = (int)$_POST["id"];
if (0 === $id) {
echo "NOT Numeric";
}
else {
echo "Numeric";
}
?>
*Note: When casting a value please be sure to cast the value to the correct type.
If you would like to learn more about Types and Casting, here are some links to sections of the PHP Manual:
Types
Type Casting
Variable Handling Functions



PDF













