Data types… Oh boy.
PHP is a “loosely typed” language. That is, it doesn’t force you to tell the computer what a variable is when you declare it. Many scripting languages are this way - JavaScript for example, and even python if memory serves correctly. The problem though is sometimes it is important to know not only the value of a variable, but what the variable is.
A variable’s datatype is what the variable is. Datatypes are pretty consistent between programming languages, at a minimum there will be integers, booleans, strings, floats and nulls. Most languages have arrays and objects as well.
When you ask PHP to compare two variables it has get them both to the same datatype to do the comparison. This happens silently in the background. Most of the time things will work out as you expect, but not always.
A classic example comes out of [fphp]strpos[/fphp] This function returns the index position of a string in another string. And like most computer indexes it starts with 0, so if the string is at the start strpos returns 0. If strpos can’t find the string you’re looking for it returns false.
Here’s the trap. 0, ‘0’, false, null, all loosely evaluate to false. That is, 0 == false.
Live code…
$a = 'pot';
$b = 'potato';
if (strpos($b, $a) == 0) {
echo 'This will print.'; // Because 0 == 0 is true.
}
$a = hot;
if (strpos($b, $a) == 0) {
echo 'This will print'; // Because, false == 0 is also true. Unintuitive yes, but that's how datatypes can be.
}
if (strpos($b, $a) === 0 {
echo 'This will not print'; // Because, false === 0 is false. The third = is strict comparison.
}
if (strpos($b, $a) === false) {
echo 'This will print'; // Because false === false.
}
$a = 'pot'
if (strpos($b, $a) === false) {
echo 'This will not print'; // Because now strpos returns 0 - 'pot' is at the start of 'potato'. The eval will be 0 === false, which isn't true.
}
This is where strict comparison comes in - for those times when you need to know not only what the variable’s value is, but what type of variable is it. For a strict comparison to be true both value and type must match.
To recap…
if (0 == false) {
echo 'true'; // Echoes true - because this is true.
}
if ( 0 === false ) {
echo 'true'; // Nothing echoed - this line will be skipped because the condition is false.
}
if ( false === false ) {
echo 'true'; // This however is true.
}
if (true == 'a string') {
echo 'true'; // Strings will evaluate true unless they parse to 0 or are empty.
}
if (false == '' ) {
echo 'true'; // This line echoes, an empty string evals false.
}
if ( false == '0' ) {
echo 'true'; // But so will this, so be careful.
}
if ( false == '000' ) {
echo 'true'; // Also parses to 0.
}
If this seems intimidating, well it can be. PHP tries pretty hard to keep this from coming up but sometimes it’s unavoidable. In strongly typed languages such as C or Java you must declare the type of variable. That is a blessing and a curse since you don’t hit corner cases like this, yet doing things in the first place is more difficult.
Off Topic:
Kalon, being rude to others hardly encourages anyone to be nice to you. If you don’t want to answer a new user’s question that’s fine, but posting RTFM is hardly constructive and ‘fine’ was not the f word you were looking for, everyone knows that.