Because PHP is loosely typed sometimes how it treats and converts variables can be confusing. However, by running a simple test script we can get a better idea of how things work.
The tests below run three tests for each variable:
- Run an equals operator (==)
- Run an identical operator (===)
- Cast to a bool and the examine the resulting variable
Here’s the code:
<?php
$vars['BOOL_TRUE'] = true;
$vars['BOOL_FALSE'] = false;
$vars['INT_0'] = 0;
$vars['INT_1'] = 1;
$vars['STRING_0'] = '0';
$vars['STRING_1'] = '1';
$vars['STRING'] = 'foo';
$vars['EMPTY_STRING'] = '';
$vars['POSITIVE_INT'] = 42;
$vars['NEGATIVE_INT'] = -42;
$vars['ZERO_FLOAT'] = 0.0000;
$vars['POSITIVE_FLOAT'] = 42.1;
$vars['NEGATIVE_FLOAT'] = -42.1;
$vars['NULL'] = null;
echo '<pre>';
foreach($vars as $key => $value) {
if($value == true) {
echo "$key: " . 'if($value) returns true<br />';
}
else {
echo "$key: " . 'if($value) returns false<br />';
}
if($value === true) {
echo "$key: " . 'if($value === true) returns true<br />';
}
else {
echo "$key: " . 'if($value === true) returns false<br />';
}
$cast = (bool) $value;
var_dump($cast);
echo '</pre>';
}
?>
The output looks like this:
BOOL_TRUE: if($value) returns true
BOOL_TRUE: if($value === true) returns true
bool(true)
BOOL_FALSE: if($value) returns false
BOOL_FALSE: if($value === true) returns false
bool(false)
INT_0: if($value) returns false
INT_0: if($value === true) returns false
bool(false)
INT_1: if($value) returns true
INT_1: if($value === true) returns false
bool(true)
STRING_0: if($value) returns false
STRING_0: if($value === true) returns false
bool(false)
STRING_1: if($value) returns true
STRING_1: if($value === true) returns false
bool(true)
STRING: if($value) returns true
STRING: if($value === true) returns false
bool(true)
EMPTY_STRING: if($value) returns false
EMPTY_STRING: if($value === true) returns false
bool(false)
POSITIVE_INT: if($value) returns true
POSITIVE_INT: if($value === true) returns false
bool(true)
NEGATIVE_INT: if($value) returns true
NEGATIVE_INT: if($value === true) returns false
bool(true)
ZERO_FLOAT: if($value) returns false
ZERO_FLOAT: if($value === true) returns false
bool(false)
POSITIVE_FLOAT: if($value) returns true
POSITIVE_FLOAT: if($value === true) returns false
bool(true)
NEGATIVE_FLOAT: if($value) returns true
NEGATIVE_FLOAT: if($value === true) returns false
bool(true)
NULL: if($value) returns false
NULL: if($value === true) returns false
bool(false)
Here are some things to bear in mind when using the == operator:
- All non-empty strings return true
- Any number other than 0 (even if zero is expressed as '0' or '0.0') return true
Other useful notes:
- Only true will return true when using ===, everything else is false
- null always returns false
- The true and false keywords are not case-sensitive (as is null)
- Casting to bool always matches what == would return
Finally, according to the PHP docs, these also return false: an array with zero elements, an object with zero member variables (PHP 4 only) and SimpleXML objects created from empty tags.
Tim Bennett is a web designer and developer. He has a First Class Honours degree in Computing from
Leeds Metropolitan University and currently runs his own one-man web design company, Texelate.