Variable Value Truthiness and Identical Operator

suggest change

In 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:

$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
$var = -1;
$var_is_true = ($var == true); // true
$var = 99;
$var_is_true = ($var == true); // true
$var = 0;
$var_is_true = ($var == true); // false
$var = null;
$var_is_true = ($var == true); // false
$var_is_false = ($var == false); // true
$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
$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:

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
}

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents