If - Else
Part of the Fundamentals section of Coddy's PHP journey — lesson 29 of 71.
Sometimes you need your code to do one thing when a condition is true, and something different when it's false. The else clause lets you handle both cases.
Add else after your if block to specify what happens when the condition is false:
<?php
$age = 15;
if ($age >= 18) {
echo "You can vote";
} else {
echo "You cannot vote yet";
}
?>Since $age is 15, the condition is false, so PHP runs the else block and prints "You cannot vote yet".
For more than two possibilities, chain conditions using elseif:
<?php
$score = 75;
if ($score >= 90) {
echo "Grade: A";
} elseif ($score >= 80) {
echo "Grade: B";
} elseif ($score >= 70) {
echo "Grade: C";
} else {
echo "Grade: F";
}
?>PHP checks each condition from top to bottom and runs the first block that matches. Once a match is found, it skips all remaining conditions. Here, 75 fails the first two checks but passes $score >= 70, so "Grade: C" is printed.
Challenge
EasyRead two integers from input: a temperature and a hour (in 24-hour format, 0-23).
Determine the appropriate message based on the following conditions:
- If the temperature is below 0, print
Freezing conditions - Otherwise, if the temperature is below 15, print
Cold weather - Otherwise, if the temperature is below 25, print
Pleasant weather - Otherwise, print
Hot weather
Then, on a new line, print a greeting based on the hour:
- If the hour is less than 12, print
Good morning! - Otherwise, if the hour is less than 18, print
Good afternoon! - Otherwise, print
Good evening!
Example:
If the inputs are 20 and 9, the output should be:
Pleasant weather
Good morning!If the inputs are -5 and 14, the output should be:
Freezing conditions
Good afternoon!Cheat sheet
The else clause specifies what happens when an if condition is false:
<?php
$age = 15;
if ($age >= 18) {
echo "You can vote";
} else {
echo "You cannot vote yet";
}
?>Use elseif to chain multiple conditions:
<?php
$score = 75;
if ($score >= 90) {
echo "Grade: A";
} elseif ($score >= 80) {
echo "Grade: B";
} elseif ($score >= 70) {
echo "Grade: C";
} else {
echo "Grade: F";
}
?>PHP checks conditions from top to bottom and executes the first matching block, then skips the rest.
Try it yourself
<?php
// Read input
$temperature = intval(fgets(STDIN));
$hour = intval(fgets(STDIN));
// TODO: Write your code below
// Determine the weather message based on temperature
// Determine the greeting based on hour
?>This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
4Comparison & Logical Operators
Comparison OperatorsEquality & IdentityLogical Operators Part 1Logical Operators Part 2Recap - Simple Logic2Variables and Data Types
NumbersStrings and QuotesBooleansNaming ConventionsRecap - Variable InitEmpty VariablesString ConcatenationGetting User InputCast to Different Types5Conditional Logic
If StatementIf - ElseThe Ternary OperatorNull Coalescing OperatorSwitch StatementRecap - Making Decisions3Basic Operators
Arithmetic OperatorsModulo OperatorExponentiation OperatorCombined AssignmentIncrement/DecrementOperator PrecedenceRecap - Simple CalculationsString Operators