if / elseif / else — truthiness rules in PHP
Concept
PHP's if/elseif/else control structure evaluates conditions using loose truthiness rules — not strict boolean comparison. PHP evaluates a value as false when: it is false, null, 0, 0.0, "0", "" (empty string), or [] (empty array). Everything else is truthy. This diverges from many other languages and is a common source of bugs.
The classic pitfall: if ($result) where $result is the string "0" from a database column — it evaluates to false even though the value exists and is meaningful. Always use strict comparison (=== null, === false, === 0) at critical decision points.
elseif vs else if: In if/endif syntax they differ — elseif is a single keyword; else if is else { if {} }. In if {} brace syntax they behave identically. Use elseif for consistency.
Alternative syntax (if(): ... endif;) is meant for template files where PHP and HTML are mixed. In pure PHP class files, always use brace syntax.
Truthiness table to memorize: 0 is false, "0" is false, "" is false, " " (space) is TRUE, "false" (the string) is TRUE, [] is false, [0] is TRUE.
Code Example
<?php
declare(strict_types=1);
// Truthiness gotchas
var_dump((bool) 0); // false
var_dump((bool) "0"); // false ← string zero is falsy!
var_dump((bool) ""); // false
var_dump((bool) " "); // true ← space is truthy
var_dump((bool) "false"); // true ← the string "false" is truthy!
var_dump((bool) []); // false
var_dump((bool) null); // false
// The "0" bug — common with DB results
$dbValue = "0"; // e.g., status column
if ($dbValue) {
echo "Has value"; // NEVER RUNS — "0" is falsy
}
// Fix: explicit comparison
if ($dbValue !== null && $dbValue !== '') {
echo "Has value"; // runs correctly
}
// if/elseif/else structure
function classifyAge(int $age): string
{
if ($age < 0) {
return 'invalid';
} elseif ($age < 13) {
return 'child';
} elseif ($age < 18) {
return 'teenager';
} elseif ($age < 65) {
return 'adult';
} else {
return 'senior';
}
}
// Strict comparison avoids truthiness surprises
$value = getUserInput(); // might return null, 0, "", or real data
if ($value === null) {
echo "Not provided";
} elseif ($value === 0 || $value === '') {
echo "Zero or empty";
} else {
echo "Has value: $value";
}
// Alternative syntax — for templates only
?>
<?php if ($loggedIn): ?>
<p>Welcome back!</p>
<?php elseif ($registered): ?>
<p>Please log in.</p>
<?php else: ?>
<p>Please register.</p>
<?php endif; ?>Interview Q&A
Q: What values are considered falsy in PHP, and how does this differ from strict false?
PHP's falsy values: false, null, 0 (integer), 0.0 (float), "0" (string zero), "" (empty string), [] (empty array), and SimpleXMLElement with no children. Everything else is truthy — including the string "false", any non-zero number, non-empty arrays, and objects. The key divergence from strict false: 0, null, "0", and "" are all falsy but not false. Use === false when you need to distinguish "function returned false as an error signal" from "function returned zero or null as a valid result."
Q: Why is using a loose if ($result) check on a database query result dangerous?
Database queries often return numeric strings, integer zeros, or null. "0" is truthy in Python and JavaScript but falsy in PHP. If a query returns "0" as a valid status code, if ($result) silently treats it as "no result". Similarly, if ($count) fails when count is 0 even though 0 is a valid count. Always use explicit comparisons: if ($result !== null && $result !== false) for existence checks, if ($result !== false) for functions that use false as an error signal.