Step 1 of 5 · Reading · ~4 min
Learn
Control Flow
Conditionals
A conditional asks a yes-or-no question and runs one branch. In PHP the hard part is not the syntax — it is knowing exactly which values count as "yes".
if / elseif / else
<?php
$kg = 12.5;
if ($kg > 20) {
echo "freight\n";
} elseif ($kg > 5) {
echo "parcel\n";
} else {
echo "letter\n";
}
//> parcel
Branches are tested top to bottom and the first match wins; everything below it is skipped. That ordering is not a detail — it is the whole design of the structure.
Move the 15 test below the 3 test in that diagram and 15 comes out as Fizz, because the first matching branch wins. The most specific condition always goes first.
PHP spells the middle keyword elseif, one word. else if as two words also works inside braces, but it breaks in the alternative if: ... endif; template syntax, so prefer elseif everywhere.
Which values count as "yes"?
PHP has an unusually long list of falsy values. Everything not on the list is truthy.
| Falsy | Truthy, and surprising |
|---|---|
false | "0.0" — a non-empty string |
0 and 0.0 | " " — a single space |
"" — the empty string | "false" — the word |
"0" — the string zero | -1 — negative is still non-zero |
null | [0] — an array holding one falsy value |
[] — the empty array |
The string "0" is the one that catches everyone: it is truthy in almost every other language and falsy here.
Why === and not ==
== converts types before comparing. === compares type and value, and never converts.
<?php
var_dump("1" == "01"); // both look numeric, so they compare as numbers
var_dump("1" === "01");
var_dump(0 == "0");
var_dump(0 === "0");
var_dump(null == false);
var_dump(null === false);
//> bool(true)
//> bool(false)
//> bool(true)
//> bool(false)
//> bool(true)
//> bool(false)
✗ Loose == | ✓ Strict === |
|---|---|
"10" == "1e1" is true | "10" === "1e1" is false |
0 == "abc" is true on PHP 7.4 | 0 === "abc" is false on every version |
That second row is worth pinning down. PHP 8 changed 0 == "abc" to false; this course's grader runs PHP 7.4, where it is still true. Using === means you never have to remember which version you are standing on.
Typing tip: === is the equals key pressed three times with no spaces between.
switch compares loosely too
switch uses == internally, so all of the above applies to it:
<?php
$code = "1";
switch ($code) {
case 1: echo "matched int 1\n"; break;
default: echo "no match\n";
}
//> matched int 1
Each case also falls through into the next one unless you write break.
Grader note: PHP 8 added
match, an expression that returns a value, needs nobreak, and compares with===. It is the modern replacement forswitch— and it is a parse error on this course's PHP 7.4 grader. Learn it for real codebases; writeif/elseif/elseorswitchin the exercises here.
Two shorthands worth knowing
<?php
$options = ["mode" => null];
echo ($options["mode"] ?? "standard"), "\n";
$label = "";
echo ($label ?: "unnamed"), "\n";
//> standard
//> unnamed
?? uses the right-hand side only when the left is null — including when an array key is missing, and without complaining about it. ?: uses the right-hand side when the left is any falsy value. That difference matters the moment 0 or "" is a legitimate answer.
Your exercise
FizzBuzz Single reads one integer and prints FizzBuzz, Fizz, Buzz, or the number itself.
The starter reads the value; the job is the if/elseif/else chain built on %. The mistake the grader catches is ordering: the very first test feeds 15, and if you check % 3 === 0 before % 15 === 0 your program prints Fizz and fails on test one. The second trap is writing three separate if statements instead of one elseif chain — 15 then prints three lines where exactly one is expected. Print one word (or the number), follow it with "\n", and print nothing else.
Discussion
Ask a question, share an insight, or help someone who’s stuck.
Sign in to post a comment or reply.
Loading…