PHP Comparison Logic Quiz
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.
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?
Question 2
Debug logs show this snippet. What appears in the terminal?
<?php
var_dump('15' == 15);
var_dump('15' === 15);
?>Question 3
A reviewer asked about the spaceship operator. What does this code print?
<?php
$a = 5;
$b = '5';
echo $a <=> $b;
?>Question 4
When checking case-sensitive product codes sent as strings, why do architects recommend strcmp over ==?
Question 5
A teammate demonstrates falsy pitfalls. What does this snippet output?
<?php
var_dump('0' == 0);
var_dump('0' === 0);
?>Question 6
Incident response highlights why switch requires caution. What line prints?
<?php
$value = '0e1234';
switch ($value) {
case 0:
echo 'zero branch';
break;
default:
echo 'default branch';
}
?>Question 7
Why do code reviews require a justification when someone uses == in conditionals?
Question 8
What does this membership check print?
<?php
$allowed = [0, 1];
echo in_array('0', $allowed, true) ? 'allowed' : 'denied';
?>Question 9
When writing comparator callbacks for usort, why must the function return -1, 0, or 1 consistently?
Question 10
A teammate demonstrates boolean coercion. What prints?
<?php
var_dump(true == '1');
var_dump(true === '1');
?>Question 11
When comparing floating-point totals from two services, why should reviewers ask for a tolerance threshold instead of direct equality?
Question 12
What does this tolerance snippet output?
<?php
$a = 0.1 + 0.2;
$b = 0.3;
echo abs($a - $b) < 0.0001 ? 'close' : 'far';
?>Question 13
Why do naming conventions recommend prefixing boolean variables with verbs such as $isActive or $canShip when writing comparison-heavy code?
Question 14
A developer evaluates locale-sensitive strings. What does this snippet print?
<?php
setlocale(LC_COLLATE, 'C');
echo strcasecmp('straße', 'STRASSE') === 0 ? 'equal' : 'different';
?>Question 15
During API reviews, why do teams document whether comparison decisions should be lexicographic or numeric when interpreting IDs?
Question 16
What does this snippet reveal about boolval?
<?php
echo boolval('false') ? 'true branch' : 'false branch';
?>Question 17
Why is logging both operands of a critical comparison useful during incident investigations?
Question 18
A pull request shows this helper. What does it print?
<?php
$input = '15';
echo filter_var($input, FILTER_VALIDATE_INT) === false ? 'reject' : 'accept';
?>Question 19
When comparing version strings, why is version_compare preferred over manual string operators?
Question 20
What is echoed by this null-coalescing comparison?
<?php
$value = '';
echo $value === '' ? 'empty string' : 'other';
?>Question 21
When reading SQL result sets, why should engineers avoid comparing numeric columns as strings when they come from loosely typed drivers?
Question 22
What does this comparator reveal?
<?php
$values = [2, '02', 10];
usort($values, function ($a, $b) {
return $a <=> $b;
});
echo implode(',', $values);
?>Question 23
Why do coding standards forbid comparing DateTime instances with ==?
Question 24
A migration script checks the result of strcmp. What does it echo?
<?php
echo strcmp('005', '5') === 0 ? 'same' : 'different';
?>Question 25
Why should business rules specify whether a comparison is inclusive or exclusive when dealing with time windows?
Question 26
What does this strict inequality print?
<?php
$limit = 100;
$input = '100';
echo $input < $limit ? 'below' : 'not below';
?>Question 27
Why do API clients rely on descriptive constants (e.g., Status::SUCCESS) instead of bare integers when comparing response codes?
Question 28
What does this nullsafe comparison echo?
<?php
$user = null;
echo $user?->role === 'admin' ? 'admin' : 'guest';
?>Question 29
Product managers ask how tie-breakers work in multi-field sorts. Why do engineers often chain comparisons rather than nest if statements?
Question 30
Why do teams document the intent behind "magic" sentinel values (like -1 meaning unlimited) before comparing them inside business logic?
