Making Decisions: if, elseif, and else
At a fork in a trail, a signpost says "Summit: left. Lake: right." You read it, and take one path. An if statement is a signpost for your code.
So far, every script has run every line, top to bottom. Real programs need to choose: show "Welcome back" to someone logged in and "Please log in" to everyone else, or charge a student price to students only. That's what if is for.
The signpost
<?php
$temperature = 34;
if ($temperature > 30) {
echo "It's hot. Bring water!\n";
}
echo "Have a nice walk.\n";It's hot. Bring water!
Have a nice walk.- The condition goes in parentheses after
if. It's any expression that gives true or false (or a value PHP converts, using the truthy and falsy rules from Type Juggling). - The code in curly braces
{ }runs only when the condition is true. That's called a block. - After the block, the script carries on as usual, either way.
Indent the code inside a block by 4 spaces. PHP doesn't need it, but it shows at a glance which lines belong to the if.
else: the other path
else gives a block to run when the condition is false:
<?php
$age = 15;
if ($age >= 18) {
echo "You can vote.\n";
} else {
echo "You can vote in " . (18 - $age) . " years.\n";
}You can vote in 3 years.Exactly one of the two blocks runs, never both, never neither.
elseif: more than two paths
For several possibilities, chain conditions with elseif. PHP checks them from the top and runs the first block whose condition is true, then skips the rest:
<?php
$score = 84;
if ($score >= 90) {
$grade = "A";
} elseif ($score >= 80) {
$grade = "B";
} elseif ($score >= 70) {
$grade = "C";
} else {
$grade = "Needs improvement";
}
echo "Grade: $grade\n";Grade: B84 is also >= 70, but PHP never checks that line, since it already found a match. That's why the order matters: put the strictest condition first. If >= 70 came first, a score of 95 would get a C.
(else if, with a space, works too. Most PHP code uses elseif.)
Combining conditions
Use && (and), || (or), and ! (not) from Operators and Expressions to check several things at once:
<?php
$age = 16;
$hasPermission = true;
$isMember = false;
if ($age >= 18 || $hasPermission) {
echo "You can join the trip.\n";
}
if (!$isMember) {
echo "Sign up to get a member discount.\n";
}You can join the trip.
Sign up to get a member discount.Blocks inside blocks
An if can go inside another one. That's called nesting:
<?php
$isLoggedIn = true;
$isAdmin = false;
if ($isLoggedIn) {
echo "Welcome back!\n";
if ($isAdmin) {
echo "Admin tools are ready.\n";
}
} else {
echo "Please log in.\n";
}Welcome back!Nesting more than two or three levels deep gets hard to read. Often, && can flatten it: if ($isLoggedIn && $isAdmin).
A classic mistake: = instead of ===
<?php
$role = "student";
if ($role = "teacher") {
echo "Hello, teacher!\n";
}
echo "Role is now: $role\n";Hello, teacher!
Role is now: teacherWith a single =, the condition doesn't compare anything. It stores "teacher" in $role, and the stored value, a non-empty string, is truthy. So the block always runs, and the variable is changed too. PHP doesn't warn you, so it's worth double-checking every condition for this.
if in HTML pages
When PHP builds HTML, as in Mixing PHP and HTML, curly braces scattered between HTML tags are hard to follow. PHP has an alternative syntax for this: a colon : instead of {, and endif; instead of }:
<?php $cartCount = 0; ?>
<?php if ($cartCount > 0): ?>
<p>You have <?= $cartCount ?> items in your cart.</p>
<?php else: ?>
<p>Your cart is empty.</p>
<?php endif; ?><p>Your cart is empty.</p>It works exactly the same; it's just easier to read inside a page. Use braces in pure PHP, and this style in HTML templates.
Try it
A movie website decides what to show a visitor. Predict what it prints.
Running PHP here
Running PHP right in the browser is coming to Syntaxia soon. For now, you can read and edit the code here. If you have PHP installed on your computer (see Setting Up), save it as index.php and run it with php index.php.
Check your prediction
Enjoy the movie!
Buying for friends? Nice.
Seats: 3The rating is PG-13 and the age is over 13, so the second branch runs. For the tickets, 3 isn't more than 4, but it is more than 1. The kids' combo line is skipped, because this if has no else, and 14 isn't under 12.
Try it yourself
- Change
$ageto 10 and$withAdulttofalse. Which lines print now? - Add an
elseto the ticketsifthat printsEnjoy the show on your own., and test it with 1 ticket. - Write an
ifthat printsWeekend!when a variable$dayis"Saturday"or"Sunday".
Check your understanding
Up next
A long chain of elseifs that all check the same variable can get repetitive. PHP has two tidier tools for that, in switch and match.
