PHP
<?php
$marks = 78;
if ($marks >= 80) {
echo "Distinction";
} elseif ($marks >= 60) {
echo "First division";
} elseif ($marks >= 45) {
echo "Second division";
} else {
echo "Needs improvement";
}Note it is elseif (one word) in PHP — else if also works but only in
curly-brace style, not the alternative syntax below.
Alternative syntax for templates
When mixing PHP into HTML, the : / endif; form reads much better than
scattered braces:
PHP
<?php if ($user): ?>
<p>Welcome back, <?= htmlspecialchars($user['name']) ?>!</p>
<?php else: ?>
<a href="/login">Log in</a>
<?php endif; ?><?= ... ?> is shorthand for <?php echo ... ?>.
What counts as false
These are all falsy in PHP:
false0and0.0""and"0"← note: the string "0" is falsynull
Everything else is truthy, including "false", "0.0", and -1.
PHP
<?php
if ("0") { echo "never runs"; }
if ("0.0") { echo "this DOES run"; }
if (-1) { echo "this runs too"; }Combining conditions
PHP
<?php
if ($age >= 18 && $hasId) {
echo "Allowed";
}
if ($role === 'admin' || $role === 'staff') {
echo "Has access";
}