PHP Control Flow and Iteration Quiz
A 50-question quiz entirely built around PHP control flow and iteration, using only code-based prompts to reinforce conditionals, loops, matches, early returns, and loop controls in real scenarios.
Question 1
What does this guard clause echo?
<?php
function formatStatus(?string $status): string {
if ($status === null) {
return 'pending';
}
return strtoupper($status);
}
echo formatStatus(null);
?>Question 2
How many times does this while loop run?
<?php
$i = 0;
while ($i < 3) {
echo $i;
$i++;
}
?>Question 3
What does this for loop echo?
<?php
for ($i = 2; $i >= 0; $i--) {
echo $i;
}
?>Question 4
Which branch runs in this nested if?
<?php
$role = 'editor';
$active = true;
if ($active) {
if ($role === 'admin') {
echo 'full';
} else {
echo 'limited';
}
} else {
echo 'inactive';
}
?>Question 5
How many items reach the echo in this foreach with continue?
<?php
$tags = ['dev', 'qa', 'ops'];
foreach ($tags as $tag) {
if ($tag === 'qa') {
continue;
}
echo $tag . ' ';
}
?>Question 6
What value is echoed here?
<?php
$value = 5;
echo $value > 3 ? 'high' : 'low';
?>Question 7
What is printed from this null coalescing chain?
<?php
$value = null;
echo $value ?? 'default';
?>Question 8
Which case triggers in this switch statement?
<?php
$status = 'scheduled';
switch ($status) {
case 'draft':
echo 'hidden';
break;
case 'scheduled':
case 'pending':
echo 'waiting';
break;
default:
echo 'live';
}
?>Question 9
What does this match expression echo?
<?php
$mode = 'cli';
echo match ($mode) {
'web' => 'http',
'cli' => 'terminal',
default => 'unknown'
};
?>Question 10
How many iterations run in this do-while loop?
<?php
$count = 0;
do {
$count++;
} while ($count < 2);
echo $count;
?>Question 11
What prints from this recursive countdown?
<?php
function countdown($n) {
if ($n === 0) {
return;
}
echo $n;
countdown($n - 1);
}
countdown(3);
?>Question 12
How many items echo in this break example?
<?php
$values = [10, 20, 30];
foreach ($values as $value) {
if ($value === 20) {
break;
}
echo $value . ' ';
}
?>Question 13
What output occurs with this labeled break?
<?php
outer:
for ($i = 0; $i < 2; $i++) {
for ($j = 0; $j < 2; $j++) {
echo "{$i}{$j} ";
break outer;
}
}
?>Question 14
What does this generator yield?
<?php
function ids() {
for ($i = 1; $i <= 3; $i++) {
yield $i;
}
}
foreach (ids() as $id) {
echo $id;
}
?>Question 15
What happens in this switch without break?
<?php
$tier = 'silver';
switch ($tier) {
case 'silver':
echo 'mid';
case 'gold':
echo 'top';
break;
default:
echo 'base';
}
?>Question 16
What value does this nested ternary echo?
<?php
$score = 85;
echo $score >= 90 ? 'A' : ($score >= 80 ? 'B' : 'C');
?>Question 17
What prints from this match expression checking boolean conditions?
<?php
$value = 10;
echo match (true) {
$value < 5 => 'low',
$value === 10 => 'exact',
default => 'other'
};
?>Question 18
What does this early return function echo?
<?php
function sanitize(?string $input): string {
if ($input === null || $input === '') {
return 'n/a';
}
return trim($input);
}
echo sanitize(' data ');
?>Question 19
How many iterations increment the counter in this loop?
<?php
$count = 0;
for ($i = 0; $i < 5; $i++) {
if ($i % 2 === 0) {
continue;
}
$count++;
}
echo $count;
?>Question 20
What prints from this while loop with an internal break?
<?php
$n = 0;
while (true) {
echo $n;
$n++;
if ($n === 2) {
break;
}
}
?>Question 21
What is echoed from this nullsafe access?
<?php
$user = null;
echo $user?->team ?? 'no team';
?>Question 22
What do these nested loops echo?
<?php
for ($i = 0; $i < 2; $i++) {
for ($j = 0; $j < 2; $j++) {
echo "{$i}{$j}";
}
}
?>Question 23
What prints from this foreach by reference?
<?php
$numbers = [1, 2];
foreach ($numbers as &$n) {
$n *= 2;
}
unset($n);
echo implode(',', $numbers);
?>Question 24
What value does this foreach with continue add up to?
<?php
$total = 0;
foreach ([1, 2, 3] as $num) {
if ($num === 2) {
continue;
}
$total += $num;
}
echo $total;
?>Question 25
What output results from this match grouping?
<?php
$level = 2;
echo match ($level) {
1 => 'low',
2, 3 => 'mid',
default => 'high'
};
?>Question 26
What prints from this continue 2 example?
<?php
for ($i = 0; $i < 2; $i++) {
for ($j = 0; $j < 2; $j++) {
if ($j === 1) {
continue 2;
}
echo "{$i}{$j} ";
}
}
?>Question 27
What value echoes from this while/break combination?
<?php
$n = 5;
while ($n > 0) {
$n--;
if ($n === 2) {
break;
}
}
echo $n;
?>Question 28
What does this recursion guard output?
<?php
function crawl($depth) {
static $calls = 0;
if ($calls++ > 2) {
return;
}
echo $depth;
crawl($depth + 1);
}
crawl(1);
?>Question 29
What does this try/catch loop print?
<?php
$numbers = [1, 0, 2];
foreach ($numbers as $n) {
try {
echo 10 / $n;
} catch (DivisionByZeroError $e) {
echo 'err';
}
}
?>Question 30
Which branch runs in this combined conditional?
<?php
$flag = false;
$enabled = true;
if ($flag && $enabled) {
echo 'both';
} elseif ($enabled || $flag) {
echo 'one';
} else {
echo 'none';
}
?>Question 31
What does this generator with early return output?
<?php
function feed() {
yield 1;
return;
yield 2;
}
foreach (feed() as $value) {
echo $value;
}
?>Question 32
What does this goto example print?
<?php
$i = 0;
start:
echo $i;
$i++;
if ($i < 2) {
goto start;
}
?>Question 33
What prints from this array_walk closure?
<?php
$items = ['a', 'b'];
array_walk($items, function ($value, $key) {
echo $key . ':' . $value . ' ';
});
?>Question 34
What does this generator with yield from echo?
<?php
function inner() {
yield 'x';
yield 'y';
}
function outer() {
yield 'start';
yield from inner();
yield 'end';
}
foreach (outer() as $value) {
echo $value;
}
?>Question 35
What value does this do-while guard echo?
<?php
$attempts = 0;
do {
$attempts++;
} while (false);
echo $attempts;
?>Question 36
What prints from this match on boolean expressions?
<?php
$count = 0;
$result = match (true) {
$count === 0 => 'zero',
$count > 0 => 'positive',
default => 'negative'
};
echo $result;
?>Question 37
What output is produced by this foreach with break?
<?php
$rows = [['id' => 1], ['id' => 2], ['id' => 3]];
foreach ($rows as $row) {
echo $row['id'];
if ($row['id'] === 2) {
break;
}
}
?>Question 38
What does this nested ternary with parentheses echo?
<?php
$flag = false;
$message = $flag ? 'on' : ($flag === false ? 'off' : 'maybe');
echo $message;
?>Question 39
What sequence is echoed from this while loop with continue?
<?php
$i = 0;
while ($i < 4) {
$i++;
if ($i % 2 === 0) {
continue;
}
echo $i;
}
?>Question 40
What order does this reverse for loop print?
<?php
$data = ['a', 'b', 'c'];
for ($i = count($data) - 1; $i >= 0; $i--) {
echo $data[$i];
}
?>Question 41
What does this array_filter callback output?
<?php
$logs = ['info', 'warn', 'error'];
print_r(array_filter($logs, fn ($entry) => $entry !== 'info'));
?>Question 42
What does this for loop with continue 2 echo?
<?php
$total = 0;
for ($i = 1; $i <= 3; $i++) {
switch ($i) {
case 2:
continue 2;
}
$total += $i;
}
echo $total;
?>Question 43
What sequence does this while loop echo?
<?php
$queue = ['sync', 'process'];
while (($task = array_shift($queue)) !== null) {
echo $task;
if ($task === 'sync') {
continue;
}
break;
}
?>Question 44
What does this match-based classifier echo?
<?php
function classify(int $n): string {
return match (true) {
$n % 2 === 0 => 'even',
$n > 10 => 'big',
default => 'odd'
};
}
echo classify(12);
?>Question 45
What total does this do-while accumulate?
<?php
$sum = 0;
$i = 1;
do {
$sum += $i;
$i++;
} while ($i <= 3);
echo $sum;
?>Question 46
What prints from this foreach that unsets an element?
<?php
$values = [1, 2, 3];
foreach ($values as $index => $value) {
if ($index === 1) {
unset($values[$index]);
continue;
}
echo $value;
}
?>Question 47
What sequence is emitted from this dual-counter for loop?
<?php
for ($i = 0, $j = 3; $i < 3 && $j > 0; $i++, $j--) {
echo "{$i}{$j} ";
}
?>Question 48
What sum does this generator/foreach combination produce?
<?php
function steps(int $max) {
for ($i = 0; $i < $max; $i++) {
yield $i;
}
}
$sum = 0;
foreach (steps(3) as $step) {
if ($step === 1) {
continue;
}
$sum += $step;
}
echo $sum;
?>Question 49
What string is built by this foreach with continue?
<?php
$tasks = ['build', 'deploy', 'verify'];
$output = '';
foreach ($tasks as $task) {
if ($task === 'deploy') {
$output .= '->';
continue;
}
$output .= $task;
}
echo $output;
?>Question 50
What value does this generator search loop echo?
<?php
function iterate(int $n) {
for ($i = 0; $i < $n; $i++) {
yield $i;
}
}
$found = null;
foreach (iterate(4) as $v) {
if ($v === 2) {
$found = $v;
break;
}
}
echo $found;
?>