Operators
Chapter 2 covered comparing values with == and ===. This chapter covers the full range of operators PHP offers, and the control structures that actually use them to change what a script does based on those comparisons.
Comparison Operators
| Operator | Meaning |
|---|---|
| == | Equal (loose, juggles types — Chapter 2) |
| === | Identical (strict, no juggling) |
| != / <> | Not equal (loose) |
| !== | Not identical (strict) |
| > / < / >= / <= | Greater than, less than, and their "or equal" variants |
| <=> | Spaceship operator — returns -1, 0, or 1 for less/equal/greater, genuinely useful in custom sorting |
Logical Operators
&& (AND), || (OR), and ! (NOT) combine and negate boolean expressions — the building blocks behind any condition more complex than a single comparison.
if / elseif / else
PHP checks each condition in order, top to bottom, and runs the block belonging to the first one that evaluates to true — every subsequent elseif/else is skipped entirely once a match is found, even if a later condition would also have been true.
elseif and else if behave identically in almost every practical case, but technically else if is actually an else block containing a nested if statement — relevant specifically when using PHP's alternative colon syntax (covered in templates), where only the single-word elseif is valid. Sticking with elseif consistently avoids needing to remember this distinction at all.
The Ternary Operator — A Compact if/else
The ternary operator condenses a simple if/else assignment into one line. The null coalescing operator (??) is a closely related, extremely common shorthand specifically for "use this value, or a fallback if it doesn't exist/is null" — genuinely useful and previewed here ahead of full coverage when $_GET/$_POST are properly introduced in Chapter 8.
switch — Comparing One Value Against Several Options
break, PHP continues executing every subsequent case's code, regardless of whether its own condition matched — this "fall-through" behaviour is occasionally used deliberately (multiple case labels sharing one block of code), but is far more often an accidental bug when a break is simply forgotten on a case that genuinely needed one.
When switch Is Better Than a Long elseif Chain
Both accomplish the same thing for comparing one variable against many possible exact values — switch is generally considered more readable once there are more than roughly 3-4 options, since it avoids repeating the variable name and comparison operator in every single condition.
Coding Challenges
Write an if/elseif/else chain that takes a temperature in Celsius (use a variable, try a few different values) and echoes "Freezing", "Cold", "Mild", or "Hot" based on reasonable thresholds you choose yourself.
📄 View solutionUsing the ternary operator, write a single line that assigns "Pass" or "Fail" to a variable based on whether a score variable is 50 or above. Then write the equivalent using a full if/else block, and compare the two.
📄 View solutionWrite a switch statement that takes a variable holding a month number (1-12) and echoes the correct season ("Winter", "Spring", "Summer", "Autumn") — group multiple case labels together for months sharing a season, using fall-through deliberately.
📄 View solutionChapter 3 Quick Reference
- Comparison operators: ==/===, !=/!==, >/</>=/<=, <=> (spaceship)
- Logical operators: && (AND), || (OR), ! (NOT)
- if/elseif/else — runs the FIRST matching block only, top to bottom, skipping the rest
- Ternary (cond ? a : b) — compact if/else for simple assignments
- ?? (null coalescing) — value, or a fallback if it's null/unset; previewed here, full coverage in Chapter 8
- switch/case/break/default — compares one value against several exact options
- Forgetting break causes fall-through — occasionally deliberate, more often an accidental bug
- Next chapter: loops — for, while, foreach