if and elseif in PHP
Branching on a value with PHP's one-word elseif and the ternary.
<?php
$hour = 14;
// elseif is one word in PHP
if ($hour < 12) {
$greeting = "good morning";
} elseif ($hour < 18) {
$greeting = "good afternoon";
} else {
$greeting = "good evening";
}
echo $greeting, PHP_EOL;
echo $hour >= 9 && $hour < 17 ? "office hours" : "closed", PHP_EOL;
How it works
- The chain tests
$houragainst noon and 18:00, binding one greeting. elseifis a single keyword in PHP, unlike the two-word forms elsewhere.- A ternary with
&&condenses the office-hours check to one line.
Keywords and builtins used here
echoelseelseifif
The run, in numbers
- Lines
- 14
- Characters to type
- 280
- Tokens
- 67
- Three-star pace
- 85 tpm
At the three-star pace of 85 tokens a minute, this run takes about 47 seconds.
Step 1 of 3 in Control flow, step 4 of 27 in Language basics.