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, except the string'0'. A string of spaces such as' 'equates totrue. - 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
trueif 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
nullequates 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
trueif they are nonzero, while zero values equate tofalse.NAN(PHP's Not-a-Number) equates totrue, i.e.NAN == trueistrue, becauseNANis a nonzero floating-point value. Since PHP 8.1 this comparison warns thatNANwas coerced to bool.- Zero-values include both +0 and -0 as defined by IEEE 754. PHP does not distinguish between +0 and -0, i.e.
floatval('0') == floatval('-0')istrue.floatval('0') === floatval('-0')is alsotrue. Bothfloatval('0') == falseandfloatval('-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); // true, because the comparison itself is true
$var_is_false = ($var == false); // false
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 and stripos functions also take a third parameter, offset. When it is set, the search starts that many characters from the beginning of the string. Since PHP 7.1 a negative offset counts back from the end. Before that, the offset could not be negative.
The function can return:
0if$needleis found at the beginning of$haystack;- a non-zero integer specifying the index if
$needleis found somewhere other than the beginning in$haystack; - and the value
falseif$needleis 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 = strpos($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 = strpos($haystack, $needle);
if ($idx !== false)
{
// logic for when $needle found in $haystack
}
else
{
// logic for when $needle not found in $haystack
}