Introduction
Programs make decisions. A shop gives a discount only above a certain price, a login page checks a password, and a grade program turns a score into a letter. To decide, a program needs operators that compare values and combine yes or no answers, and conditions such as if that choose which code runs. This lesson covers both, with the special PHP operators <=>, ?? and match.
How it works
Arithmetic operators are +, -, *, /, % (remainder) and ** (power). The short forms +=, -=, *= and .= change a variable in place, and $i++ adds one.
Comparison operators give true or false: <, >, <=, >=, == (equal after type conversion) and === (equal and the same type). The matching "not equal" operators are != and !==. In PHP 8, "10" == "1e1" is true because both look like numbers, but "abc" === "abc" shows the safe way: use === when you want an exact comparison. The spaceship operator <=> gives -1, 0 or 1 and is very useful for sorting.
Logical operators combine conditions: && (and), || (or) and ! (not). They stop early: if the left side of && is false, the right side is never checked.
The if statement runs a block when its condition is true. Add elseif and else for more choices; PHP checks them from top to bottom and runs only the first block that matches:
if ($score >= 90) { $grade = "A"; }
elseif ($score >= 80) { $grade = "B"; }
else { $grade = "F"; }
The ternary operator is a short if: $x % 2 == 0 ? "even" : "odd". The null coalescing operator ?? gives the left value if it exists and is not null, and otherwise the right value, so $ages["Zed"] ?? 0 is a safe way to read a missing item.
The switch statement compares one value with many cases and needs break to stop; without it the code falls into the next case. The newer match expression is shorter and safer: it compares strictly, returns a value, and never falls through. match (true) lets you use conditions as arms.
Remember that in PHP these values are "falsy": false, 0, 0.0, "", "0", an empty array and null. Everything else is true.
Example
<?php
$score = (int) trim(fgets(STDIN));
if ($score >= 90) {
$grade = "A";
} elseif ($score >= 80) {
$grade = "B";
} elseif ($score >= 70) {
$grade = "C";
} else {
$grade = "F";
}
echo "Score $score gives grade $grade\n";
echo $score % 2 == 0 ? "even" : "odd", "\n";
$pass = $score >= 50 && $score <= 100;
echo $pass ? "passed" : "failed", "\n";
echo match (true) {
$score >= 90 => "excellent",
$score >= 70 => "good",
default => "keep going",
}, "\n";
var_dump(5 <=> 3, "10" == "1e1", "abc" === "abc", null ?? "default");
$day = 3;
switch ($day) {
case 1:
echo "Mon\n";
break;
case 3:
echo "Wed\n";
break;
default:
echo "Other\n";
}
echo 2 ** 10, " ", 17 % 5, " ", intdiv(17, 5), "\n";Input used: 72
Line by line
- The score is read and cast to an int.
- The
ifchain checks from 90 downwards, and the first true condition sets the grade. %and the ternary operator say whether the score is even or odd.&&checks that the score is between 50 and 100.match (true)goes through the conditions and gives the text of the first one that is true.var_dumpshows the result of<=>, a loose comparison, a strict comparison and??.switchfinds the case for day 3 andbreakstops it.- The last line shows power, remainder and whole-number division.
Expected result
With the input 72, it prints:
Score 72 gives grade C
even
passed
good
int(1)
bool(true)
bool(true)
string(7) "default"
Wed
1024 2 3
In the real world
Almost every feature of a web application is a condition: is the user logged in, is the cart empty, is the date in the past, did the payment work. Clear conditions keep programs correct. Sorting code uses <=> to compare records, and configuration code uses ?? to give default values.
Common mistakes
- Writing
=instead of==or===in a condition. The first one stores a value. - Forgetting
breakin aswitch, so the next case runs too. - Using
==when types matter.0 == "a"is false in PHP 8, but loose comparisons still surprise people. - Writing an
ifchain in the wrong order, so that a wide condition catches everything first.
Best practices
- Prefer
===and!==. - Check the strictest condition first in an
elseifchain. - Use
matchinstead ofswitchwhen you need a value. - Use
??to provide default values for missing data.
Summary
Operators calculate and compare, and &&, || and ! combine conditions. if, elseif and else choose a block, switch and match choose between many values, and the ternary operator is a short if. Use === for exact comparison, <=> for sorting and ?? for defaults.
Quick check
Key takeaways
- Conditions choose which code runs, and the first true block of an elseif chain wins.
- Use the strict operators when the type matters.
- The ternary, null coalescing and match give short and safe code.
Practice problems for this lesson
Was this lesson clear?
My notes
Write down what you want to remember from this lesson. Notes are saved on this device as you type, and appear on your dashboard.