Variable Value Truthiness and Identical Operator
suggest changeIn PHP, variable values have an associated “truthiness” so even non-boolean values will equate to true
or false
. This allows any variable to be used in a conditional block, e.g.
if ($var == true) { /* explicit version */ }
if ($var) { /* $var == true is implicit */ }
Here are some fundamental rules for different types of variable values:
- Strings with non-zero length equate to
true
including strings containing only whitepace such as' '
. - Empty strings
''
equate tofalse
.
$var = '';
$var_is_true = ($var == true); // false
$var_is_false = ($var == false); // true
$var = ' ';
$var_is_true = ($var == true); // true
$var_is_false = ($var == false); // false
- Integers equate to
true
if they are nonzero, while zero equates tofalse
.
$var = -1;
$var_is_true = ($var == true); // true
$var = 99;
$var_is_true = ($var == true); // true
$var = 0;
$var_is_true = ($var == true); // false
null
equates tofalse
$var = null;
$var_is_true = ($var == true); // false
$var_is_false = ($var == false); // true
- Empty strings
''
and string zero'0'
equate tofalse
.
$var = '';
$var_is_true = ($var == true); // false
$var_is_false = ($var == false); // true
$var = '0';
$var_is_true = ($var == true); // false
$var_is_false = ($var == false); // true
- Floating-point values equate to
true
if they are nonzero, while zero values equates tofalse
.
* `NAN` (PHP's Not-a-Number) equates to `true`, i.e. `NAN == true` is `true`. This is because `NAN` is a _nonzero_ floating-point value.
* Zero-values include both +0 and -0 as defined by IEEE 754. PHP does not distinguish between +0 and -0 in its double-precision floating-point, i.e. `floatval('0') == floatval('-0')` is `true`.
* In fact, `floatval('0') === floatval('-0')`.
* Additionally, both `floatval('0') == false` and `floatval('-0') == false`.
$var = NAN;
$var_is_true = ($var == true); // true
$var_is_false = ($var == false); // false
$var = floatval('-0');
$var_is_true = ($var == true); // false
$var_is_false = ($var == false); // true
$var = floatval('0') == floatval('-0');
$var_is_true = ($var == true); // false
$var_is_false = ($var == false); // true
IDENTICAL OPERATOR
In the PHP Documentation for Comparison Operators, there is an Identical Operator ===
. This operator can be used to check whether a variable is identical to a reference value:
$var = null;
$var_is_null = $var === null; // true
$var_is_true = $var === true; // false
$var_is_false = $var === false; // false
It has a corresponding not identical operator !==
:
$var = null;
$var_is_null = $var !== null; // false
$var_is_true = $var !== true; // true
$var_is_false = $var !== false; // true
The identical operator can be used as an alternative to language functions like is_null()
.
USE CASE WITH strpos()
The strpos($haystack, $needle)
language function is used to locate the index at which $needle
occurs in $haystack
, or whether it occurs at all. The strpos()
function is case sensitive; if case-insensitive find is what you need you can go with stripos($haystack, $needle)
The strpos
& stripos
function also contains third parameter offset
(int) which if specified, search will start this number of characters counted from the beginning of the string. Unlike strrpos and strripos, the offset cannot be negative
The function can return:
0
if$needle
is found at the beginning of$haystack
;- a non-zero integer specifying the index if
$needle
is found somewhere other than the beginning in$haystack
; - and value
false
if$needle
is not found anywhere in$haystack
.
Because both 0
and false
have truthiness false
in PHP but represent distinct situations for strpos()
, it is important to distinguish between them and use the identical operator ===
to look exactly for false
and not just a value that equates to false
.
$idx = substr($haystack, $needle);
if ($idx === false)
{
// logic for when $needle not found in $haystack
}
else
{
// logic for when $needle found in $haystack
}
Alternatively, using the not identical operator:
$idx = substr($haystack, $needle);
if ($idx !== false)
{
// logic for when $needle found in $haystack
}
else
{
// logic for when $needle not found in $haystack
}