PHP Control Flow and Iteration Quiz

PHP
0 Passed
0% acceptance

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.

50 Questions
~100 minutes
1

Question 1

What does this guard clause echo?

php
<?php
function formatStatus(?string $status): string {
    if ($status === null) {
        return 'pending';
    }
    return strtoupper($status);
}
echo formatStatus(null);
?>
A
pending
B
NULL
C
status
D
PENDINGNULL
2

Question 2

How many times does this while loop run?

php
<?php
$i = 0;
while ($i < 3) {
    echo $i;
    $i++;
}
?>
A
3
B
2
C
4
D
Infinite loop
3

Question 3

What does this for loop echo?

php
<?php
for ($i = 2; $i >= 0; $i--) {
    echo $i;
}
?>
A
210
B
012
C
21
D
0
4

Question 4

Which branch runs in this nested if?

php
<?php
$role = 'editor';
$active = true;
if ($active) {
    if ($role === 'admin') {
        echo 'full';
    } else {
        echo 'limited';
    }
} else {
    echo 'inactive';
}
?>
A
limited
B
full
C
inactive
D
admin
5

Question 5

How many items reach the echo in this foreach with continue?

php
<?php
$tags = ['dev', 'qa', 'ops'];
foreach ($tags as $tag) {
    if ($tag === 'qa') {
        continue;
    }
    echo $tag . ' ';
}
?>
A
2 items
B
1 item
C
3 items
D
0 items
6

Question 6

What value is echoed here?

php
<?php
$value = 5;
echo $value > 3 ? 'high' : 'low';
?>
A
high
B
low
C
5
D
true
7

Question 7

What is printed from this null coalescing chain?

php
<?php
$value = null;
echo $value ?? 'default';
?>
A
default
B
null
C
D
Notice
8

Question 8

Which case triggers in this switch statement?

php
<?php
$status = 'scheduled';
switch ($status) {
    case 'draft':
        echo 'hidden';
        break;
    case 'scheduled':
    case 'pending':
        echo 'waiting';
        break;
    default:
        echo 'live';
}
?>
A
waiting
B
hidden
C
live
D
scheduled
9

Question 9

What does this match expression echo?

php
<?php
$mode = 'cli';
echo match ($mode) {
    'web' => 'http',
    'cli' => 'terminal',
    default => 'unknown'
};
?>
A
terminal
B
http
C
unknown
D
cli
10

Question 10

How many iterations run in this do-while loop?

php
<?php
$count = 0;
do {
    $count++;
} while ($count < 2);
echo $count;
?>
A
2
B
1
C
0
D
Infinite loop
11

Question 11

What prints from this recursive countdown?

php
<?php
function countdown($n) {
    if ($n === 0) {
        return;
    }
    echo $n;
    countdown($n - 1);
}
countdown(3);
?>
A
321
B
012
C
333
D
Infinite recursion
12

Question 12

How many items echo in this break example?

php
<?php
$values = [10, 20, 30];
foreach ($values as $value) {
    if ($value === 20) {
        break;
    }
    echo $value . ' ';
}
?>
A
1 item
B
2 items
C
3 items
D
0 items
13

Question 13

What output occurs with this labeled break?

php
<?php
outer:
for ($i = 0; $i < 2; $i++) {
    for ($j = 0; $j < 2; $j++) {
        echo "{$i}{$j} ";
        break outer;
    }
}
?>
A
00
B
00 01 10 11
C
01 10
D
No output
14

Question 14

What does this generator yield?

php
<?php
function ids() {
    for ($i = 1; $i <= 3; $i++) {
        yield $i;
    }
}
foreach (ids() as $id) {
    echo $id;
}
?>
A
123
B
321
C
111
D
No output
15

Question 15

What happens in this switch without break?

php
<?php
$tier = 'silver';
switch ($tier) {
    case 'silver':
        echo 'mid';
    case 'gold':
        echo 'top';
        break;
    default:
        echo 'base';
}
?>
A
midtop
B
mid
C
top
D
base
16

Question 16

What value does this nested ternary echo?

php
<?php
$score = 85;
echo $score >= 90 ? 'A' : ($score >= 80 ? 'B' : 'C');
?>
A
B
B
A
C
C
D
85
17

Question 17

What prints from this match expression checking boolean conditions?

php
<?php
$value = 10;
echo match (true) {
    $value < 5 => 'low',
    $value === 10 => 'exact',
    default => 'other'
};
?>
A
exact
B
low
C
other
D
true
18

Question 18

What does this early return function echo?

php
<?php
function sanitize(?string $input): string {
    if ($input === null || $input === '') {
        return 'n/a';
    }
    return trim($input);
}
echo sanitize('  data ');
?>
A
data
B
data
C
n/a
D
19

Question 19

How many iterations increment the counter in this loop?

php
<?php
$count = 0;
for ($i = 0; $i < 5; $i++) {
    if ($i % 2 === 0) {
        continue;
    }
    $count++;
}
echo $count;
?>
A
2
B
3
C
5
D
0
20

Question 20

What prints from this while loop with an internal break?

php
<?php
$n = 0;
while (true) {
    echo $n;
    $n++;
    if ($n === 2) {
        break;
    }
}
?>
A
01
B
012
C
1
D
Infinite loop
21

Question 21

What is echoed from this nullsafe access?

php
<?php
$user = null;
echo $user?->team ?? 'no team';
?>
A
no team
B
team
C
null
D
Error
22

Question 22

What do these nested loops echo?

php
<?php
for ($i = 0; $i < 2; $i++) {
    for ($j = 0; $j < 2; $j++) {
        echo "{$i}{$j}";
    }
}
?>
A
00011011
B
0011
C
0101
D
1100
23

Question 23

What prints from this foreach by reference?

php
<?php
$numbers = [1, 2];
foreach ($numbers as &$n) {
    $n *= 2;
}
unset($n);
echo implode(',', $numbers);
?>
A
2,4
B
1,2
C
4,8
D
Error
24

Question 24

What value does this foreach with continue add up to?

php
<?php
$total = 0;
foreach ([1, 2, 3] as $num) {
    if ($num === 2) {
        continue;
    }
    $total += $num;
}
echo $total;
?>
A
4
B
6
C
3
D
1
25

Question 25

What output results from this match grouping?

php
<?php
$level = 2;
echo match ($level) {
    1 => 'low',
    2, 3 => 'mid',
    default => 'high'
};
?>
A
mid
B
low
C
high
D
2
26

Question 26

What prints from this continue 2 example?

php
<?php
for ($i = 0; $i < 2; $i++) {
    for ($j = 0; $j < 2; $j++) {
        if ($j === 1) {
            continue 2;
        }
        echo "{$i}{$j} ";
    }
}
?>
A
00 10
B
00 01 10 11
C
01 11
D
No output
27

Question 27

What value echoes from this while/break combination?

php
<?php
$n = 5;
while ($n > 0) {
    $n--;
    if ($n === 2) {
        break;
    }
}
echo $n;
?>
A
2
B
5
C
0
D
3
28

Question 28

What does this recursion guard output?

php
<?php
function crawl($depth) {
    static $calls = 0;
    if ($calls++ > 2) {
        return;
    }
    echo $depth;
    crawl($depth + 1);
}
crawl(1);
?>
A
123
B
12
C
Infinite output
D
321
29

Question 29

What does this try/catch loop print?

php
<?php
$numbers = [1, 0, 2];
foreach ($numbers as $n) {
    try {
        echo 10 / $n;
    } catch (DivisionByZeroError $e) {
        echo 'err';
    }
}
?>
A
10err5
B
105
C
errerr
D
0
30

Question 30

Which branch runs in this combined conditional?

php
<?php
$flag = false;
$enabled = true;
if ($flag && $enabled) {
    echo 'both';
} elseif ($enabled || $flag) {
    echo 'one';
} else {
    echo 'none';
}
?>
A
one
B
both
C
none
D
true
31

Question 31

What does this generator with early return output?

php
<?php
function feed() {
    yield 1;
    return;
    yield 2;
}
foreach (feed() as $value) {
    echo $value;
}
?>
A
1
B
12
C
2
D
No output
32

Question 32

What does this goto example print?

php
<?php
$i = 0;
start:
echo $i;
$i++;
if ($i < 2) {
    goto start;
}
?>
A
01
B
012
C
0
D
Infinite loop
33

Question 33

What prints from this array_walk closure?

php
<?php
$items = ['a', 'b'];
array_walk($items, function ($value, $key) {
    echo $key . ':' . $value . ' ';
});
?>
A
0:a 1:b
B
a:b
C
0:1
D
ab
34

Question 34

What does this generator with yield from echo?

php
<?php
function inner() {
    yield 'x';
    yield 'y';
}
function outer() {
    yield 'start';
    yield from inner();
    yield 'end';
}
foreach (outer() as $value) {
    echo $value;
}
?>
A
startxyend
B
xy
C
startend
D
startyxend
35

Question 35

What value does this do-while guard echo?

php
<?php
$attempts = 0;
do {
    $attempts++;
} while (false);
echo $attempts;
?>
A
1
B
0
C
2
D
Infinite loop
36

Question 36

What prints from this match on boolean expressions?

php
<?php
$count = 0;
$result = match (true) {
    $count === 0 => 'zero',
    $count > 0 => 'positive',
    default => 'negative'
};
echo $result;
?>
A
zero
B
positive
C
negative
D
true
37

Question 37

What output is produced by this foreach with break?

php
<?php
$rows = [['id' => 1], ['id' => 2], ['id' => 3]];
foreach ($rows as $row) {
    echo $row['id'];
    if ($row['id'] === 2) {
        break;
    }
}
?>
A
12
B
123
C
1
D
23
38

Question 38

What does this nested ternary with parentheses echo?

php
<?php
$flag = false;
$message = $flag ? 'on' : ($flag === false ? 'off' : 'maybe');
echo $message;
?>
A
off
B
on
C
maybe
D
false
39

Question 39

What sequence is echoed from this while loop with continue?

php
<?php
$i = 0;
while ($i < 4) {
    $i++;
    if ($i % 2 === 0) {
        continue;
    }
    echo $i;
}
?>
A
13
B
1234
C
24
D
Nothing
40

Question 40

What order does this reverse for loop print?

php
<?php
$data = ['a', 'b', 'c'];
for ($i = count($data) - 1; $i >= 0; $i--) {
    echo $data[$i];
}
?>
A
cba
B
abc
C
cab
D
bca
41

Question 41

What does this array_filter callback output?

php
<?php
$logs = ['info', 'warn', 'error'];
print_r(array_filter($logs, fn ($entry) => $entry !== 'info'));
?>
A
Array ( [1] => warn [2] => error )
B
Array ( [0] => info )
C
Array ( [0] => warn [1] => error )
D
Array ( )
42

Question 42

What does this for loop with continue 2 echo?

php
<?php
$total = 0;
for ($i = 1; $i <= 3; $i++) {
    switch ($i) {
        case 2:
            continue 2;
    }
    $total += $i;
}
echo $total;
?>
A
4
B
6
C
3
D
0
43

Question 43

What sequence does this while loop echo?

php
<?php
$queue = ['sync', 'process'];
while (($task = array_shift($queue)) !== null) {
    echo $task;
    if ($task === 'sync') {
        continue;
    }
    break;
}
?>
A
syncprocess
B
sync
C
process
D
44

Question 44

What does this match-based classifier echo?

php
<?php
function classify(int $n): string {
    return match (true) {
        $n % 2 === 0 => 'even',
        $n > 10 => 'big',
        default => 'odd'
    };
}
echo classify(12);
?>
A
even
B
big
C
odd
D
12
45

Question 45

What total does this do-while accumulate?

php
<?php
$sum = 0;
$i = 1;
do {
    $sum += $i;
    $i++;
} while ($i <= 3);
echo $sum;
?>
A
6
B
3
C
1
D
0
46

Question 46

What prints from this foreach that unsets an element?

php
<?php
$values = [1, 2, 3];
foreach ($values as $index => $value) {
    if ($index === 1) {
        unset($values[$index]);
        continue;
    }
    echo $value;
}
?>
A
13
B
12
C
23
D
1
47

Question 47

What sequence is emitted from this dual-counter for loop?

php
<?php
for ($i = 0, $j = 3; $i < 3 && $j > 0; $i++, $j--) {
    echo "{$i}{$j} ";
}
?>
A
03 12 21
B
03 14 25
C
00 11 22
D
03
48

Question 48

What sum does this generator/foreach combination produce?

php
<?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;
?>
A
2
B
3
C
1
D
0
49

Question 49

What string is built by this foreach with continue?

php
<?php
$tasks = ['build', 'deploy', 'verify'];
$output = '';
foreach ($tasks as $task) {
    if ($task === 'deploy') {
        $output .= '->';
        continue;
    }
    $output .= $task;
}
echo $output;
?>
A
build->verify
B
builddeployverify
C
->deploy
D
buildverify->
50

Question 50

What value does this generator search loop echo?

php
<?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;
?>
A
2
B
3
C
null
D
0

QUIZZES IN PHP