PHP Comparison Logic Quiz

PHP
0 Passed
0% acceptance

A 30-question quiz on PHP comparison rules, strict versus loose equality, ordering operators, and guard-rail techniques so reviewers can trust every condition that protects data and features.

30 Questions
~60 minutes
1

Question 1

During an authorization review, why do staff engineers treat comparison rules as part of the security surface rather than a mere style preference?

A
Comparisons decide which code paths run, so loose logic can let untrusted input bypass permission checks.
B
Comparisons only affect performance, not security.
C
Comparisons automatically sanitize user input.
D
Comparisons are rewritten by OPCache so the original operator is irrelevant.
2

Question 2

Debug logs show this snippet. What appears in the terminal?

php
<?php
var_dump('15' == 15);
var_dump('15' === 15);
?>
A
bool(true) then bool(false)
B
bool(false) then bool(true)
C
bool(true) then bool(true)
D
bool(false) then bool(false)
3

Question 3

A reviewer asked about the spaceship operator. What does this code print?

php
<?php
$a = 5;
$b = '5';
echo $a <=> $b;
?>
A
0 because both sides evaluate to the same numeric value
B
1 because integers outrank strings
C
-1 because strings outrank integers
D
It throws a TypeError
4

Question 4

When checking case-sensitive product codes sent as strings, why do architects recommend strcmp over ==?

A
strcmp performs a binary-safe lexical comparison without type juggling, so "ab" and "AB" remain distinct.
B
strcmp automatically trims whitespace from both sides.
C
strcmp casts inputs to integers first.
D
strcmp encrypts the strings before comparing.
5

Question 5

A teammate demonstrates falsy pitfalls. What does this snippet output?

php
<?php
var_dump('0' == 0);
var_dump('0' === 0);
?>
A
bool(true) then bool(false)
B
bool(false) then bool(true)
C
bool(true) then bool(true)
D
bool(false) then bool(false)
6

Question 6

Incident response highlights why switch requires caution. What line prints?

php
<?php
$value = '0e1234';
switch ($value) {
    case 0:
        echo 'zero branch';
        break;
    default:
        echo 'default branch';
}
?>
A
zero branch
B
default branch
C
It throws a TypeError
D
It prints nothing
7

Question 7

Why do code reviews require a justification when someone uses == in conditionals?

A
Because == performs type juggling that can hide bugs, reviewers want to know the developer considered the risk consciously.
B
Because == is deprecated and will be removed.
C
Because == is slower than strict equality by orders of magnitude.
D
Because == only works on arrays.
8

Question 8

What does this membership check print?

php
<?php
$allowed = [0, 1];
echo in_array('0', $allowed, true) ? 'allowed' : 'denied';
?>
A
denied because strict mode prevents type juggling
B
allowed because strings auto-convert
C
allowed only when $allowed is sorted
D
denied only on PHP 8
9

Question 9

When writing comparator callbacks for usort, why must the function return -1, 0, or 1 consistently?

A
The sorting algorithm expects those sentinel values; anything else can leave the array in an undefined order.
B
Returning other values throws a parse error.
C
Returning 2 makes the algorithm faster.
D
Returning 0 forces the algorithm to stop.
10

Question 10

A teammate demonstrates boolean coercion. What prints?

php
<?php
var_dump(true == '1');
var_dump(true === '1');
?>
A
bool(true) then bool(false)
B
bool(false) then bool(true)
C
bool(true) then bool(true)
D
bool(false) then bool(false)
11

Question 11

When comparing floating-point totals from two services, why should reviewers ask for a tolerance threshold instead of direct equality?

A
Binary floating-point representations can differ by tiny amounts, so absolute equality may fail even when values are logically identical.
B
Floating comparisons are always faster than integers.
C
Floats cannot be compared at all.
D
Equality checks on floats are disallowed in PHP.
12

Question 12

What does this tolerance snippet output?

php
<?php
$a = 0.1 + 0.2;
$b = 0.3;
echo abs($a - $b) < 0.0001 ? 'close' : 'far';
?>
A
close
B
far
C
0
D
It throws a warning
13

Question 13

Why do naming conventions recommend prefixing boolean variables with verbs such as $isActive or $canShip when writing comparison-heavy code?

A
Verb prefixes communicate that the variable holds a truth value, making future comparisons self-explanatory.
B
PHP refuses to compare nouns.
C
Verbs automatically force strict comparison.
D
Verbs make the variable constant.
14

Question 14

A developer evaluates locale-sensitive strings. What does this snippet print?

php
<?php
setlocale(LC_COLLATE, 'C');
echo strcasecmp('straße', 'STRASSE') === 0 ? 'equal' : 'different';
?>
A
different
B
equal
C
It depends on PHP version
D
It throws a warning
15

Question 15

During API reviews, why do teams document whether comparison decisions should be lexicographic or numeric when interpreting IDs?

A
Clear documentation prevents developers from accidentally treating zero-padded strings as numbers and losing significant digits.
B
Documentation forces PHP to use strict equality.
C
Lexicographic comparisons always run faster.
D
Numeric comparisons are illegal for IDs.
16

Question 16

What does this snippet reveal about boolval?

php
<?php
echo boolval('false') ? 'true branch' : 'false branch';
?>
A
true branch
B
false branch
C
It throws a warning
D
It prints nothing
17

Question 17

Why is logging both operands of a critical comparison useful during incident investigations?

A
It shows the raw inputs and their types, helping responders confirm whether coercion or data corruption occurred.
B
Logging forces PHP to re-run the comparison.
C
Logging automatically encrypts sensitive data.
D
Logging makes comparisons strict.
18

Question 18

A pull request shows this helper. What does it print?

php
<?php
$input = '15';
echo filter_var($input, FILTER_VALIDATE_INT) === false ? 'reject' : 'accept';
?>
A
accept
B
reject
C
It throws a warning
D
It prints nothing
19

Question 19

When comparing version strings, why is version_compare preferred over manual string operators?

A
version_compare understands semantic segments, meaning "1.10" correctly ranks above "1.2".
B
It automatically downloads updates.
C
It casts versions to floats before comparing.
D
It only works for integers.
20

Question 20

What is echoed by this null-coalescing comparison?

php
<?php
$value = '';
echo $value === '' ? 'empty string' : 'other';
?>
A
empty string
B
other
C
NULL
D
It throws a warning
21

Question 21

When reading SQL result sets, why should engineers avoid comparing numeric columns as strings when they come from loosely typed drivers?

A
String comparisons may treat "02" and "2" as different even though the column is numeric, causing duplicates or missed matches.
B
Strings explode when converted.
C
Drivers refuse to return numbers.
D
Comparisons crash the PDO connection.
22

Question 22

What does this comparator reveal?

php
<?php
$values = [2, '02', 10];
usort($values, function ($a, $b) {
    return $a <=> $b;
});
echo implode(',', $values);
?>
A
2,02,10 because numeric strings are coerced to numbers during comparison
B
02,10,2 because strings always come first
C
10,2,02 because <=> sorts descending
D
It throws a warning
23

Question 23

Why do coding standards forbid comparing DateTime instances with ==?

A
== ignores timezone differences and only compares timestamps, which can hide mismatched context.
B
== is unsupported for objects.
C
== automatically mutates the objects.
D
== logs the result to disk by default.
24

Question 24

A migration script checks the result of strcmp. What does it echo?

php
<?php
echo strcmp('005', '5') === 0 ? 'same' : 'different';
?>
A
different
B
same
C
It throws a warning
D
It echoes 0
25

Question 25

Why should business rules specify whether a comparison is inclusive or exclusive when dealing with time windows?

A
Ambiguity about <= versus < leads to off-by-one errors, especially when events occur exactly on the boundary.
B
Inclusive comparisons run faster.
C
Exclusive comparisons are illegal in PHP.
D
The interpreter guesses automatically.
26

Question 26

What does this strict inequality print?

php
<?php
$limit = 100;
$input = '100';
echo $input < $limit ? 'below' : 'not below';
?>
A
not below
B
below
C
It throws a warning
D
It prints nothing
27

Question 27

Why do API clients rely on descriptive constants (e.g., Status::SUCCESS) instead of bare integers when comparing response codes?

A
Named values make comparisons self-documenting and reduce the chance of mixing unrelated numeric codes.
B
Constants cannot be compared in PHP.
C
Integers cannot be serialized.
D
Comparisons only work with strings.
28

Question 28

What does this nullsafe comparison echo?

php
<?php
$user = null;
echo $user?->role === 'admin' ? 'admin' : 'guest';
?>
A
guest
B
admin
C
It throws an error because $user is null
D
It prints nothing
29

Question 29

Product managers ask how tie-breakers work in multi-field sorts. Why do engineers often chain comparisons rather than nest if statements?

A
Chaining (or using the spaceship operator on arrays of tuples) keeps comparator logic short, consistent, and easier to audit.
B
Nested if statements are forbidden by PHP.
C
Chaining allows strict comparison only.
D
Chaining encrypts the dataset.
30

Question 30

Why do teams document the intent behind "magic" sentinel values (like -1 meaning unlimited) before comparing them inside business logic?

A
Without documentation, future maintainers may treat the sentinel as a real measurement and accidentally break comparisons or limits.
B
Sentinel values cannot be compared.
C
Sentinel values auto-expire after a day.
D
Documenting turns the sentinel into an enum automatically.

QUIZZES IN PHP