PHP Type Coversion Quiz
A 35-question quiz focused on PHP type conversion strategies so you can juggle user input, database values, and API payloads without letting silent coercion hide bugs or security issues.
Question 1
During an intake service review, why do senior engineers insist on casting incoming JSON fields to the expected types before passing them deeper into the application?
Question 2
A warehouse script trims counts from labels. What does this snippet echo?
<?php
$value = '12cases';
echo (int) $value;
?>Question 3
Support compared these two lines. Why do they log different values?
<?php
echo '5' + 4;
echo '5' . 4;
?>Question 4
Strict audits flagged a comparison using ==. Why is === safer when checking user roles stored as strings?
Question 5
A teammate wonders what FILTER_VALIDATE_INT returns. How does this snippet behave?
<?php
$result = filter_var('108', FILTER_VALIDATE_INT);
var_dump($result);
?>Question 6
During a design review, why do analysts recommend documenting expected array shapes using @var annotations such as @var array<int, string> $tags?
Question 7
What does this optional parameter snippet echo for the fallback path?
<?php
$color = null;
$color ??= 'blue';
echo $color;
?>Question 8
A CLI importer wants to ensure a token contains only digits before casting. How does this snippet help?
<?php
echo ctype_digit('401A') ? 'ok' : 'reject';
?>Question 9
When converting stored booleans, why is it risky to rely on the truthiness of strings like "false" without casting?
Question 10
New teammates compare json_decode outputs. What does this snippet display?
<?php
$payload = json_decode('{"active":true}', true);
var_dump($payload['active']);
?>Question 11
Why is number_format often used when showing floats to end users even though the internal value stays numeric?
Question 12
A debugging session prints this snippet. What appears?
<?php
$value = 0.1 + 0.2;
echo (string) $value;
?>Question 13
Security reviews often forbid using == when comparing hashed tokens. Why?
Question 14
A teammate checks how array_filter behaves with numeric strings. What prints?
<?php
$values = ['5', 5, '05'];
$filtered = array_filter($values, 'is_int');
print_r($filtered);
?>Question 15
When sanitizing query parameters, why is FILTER_SANITIZE_NUMBER_FLOAT with FILTER_FLAG_ALLOW_FRACTION useful?
Question 16
A code sample shows the risk of loose comparisons. What does it echo?
<?php
var_dump(0 == '0e12345');
?>Question 17
Documentation for a typed DTO lists ?float $amount. What should reviewers infer from that declaration?
Question 18
What does this typed property snippet print?
<?php
class Invoice {
public int $units;
}
$invoice = new Invoice();
$invoice->units = '5';
echo $invoice->units;
?>Question 19
Why do reviewers prefer explicit (float) casting before dividing integers when precise decimals are required?
Question 20
A teammate tests bool casting. What prints here?
<?php
echo (bool) "0" ? 'true' : 'false';
?>Question 21
Why is intval($value, 16) preferred over (int)$value when parsing hexadecimal IDs from hardware devices?
Question 22
A logging helper shows the risk of settype. What prints?
<?php
$value = 'status';
settype($value, 'int');
var_dump($value);
?>Question 23
When migrating legacy bool columns stored as "Y" or "N", why is a conversion map (e.g., ["Y" => true, "N" => false]) clearer than casting?
Question 24
What does this sprintf-based conversion output?
<?php
$amount = 5;
echo sprintf('%.2f', $amount);
?>Question 25
During QA, why is it important to distinguish between JSON null and the string "null" when decoding payloads?
Question 26
An onboarding kata highlights strtotime conversion. What does this snippet echo?
<?php
echo (string) strtotime('2025-01-01 00:00:00');
?>Question 27
Why do developers avoid storing resources (like cURL handles) in session data when discussing type conversion?
Question 28
A script compares integer input with a string reference. What prints?
<?php
$input = 10;
$reference = '10';
echo $input === $reference ? 'match' : 'mismatch';
?>Question 29
When integrating with older XML services, why is simplexml_load_string often cast to JSON and back before further processing?
Question 30
Why does unpacking JSON into typed DTOs help with conversion discipline compared to using associative arrays everywhere?
Question 31
In a migration guide, why must teams differentiate between (array) $object and iterator_to_array($object) when converting traversable data?
Question 32
Why is floatval() sometimes favored over (float)$value during refactors?
Question 33
A code review highlights bcadd usage. What type-related benefit does BCMath provide when summing currency as strings?
Question 34
A utility guards against double casting. What is the outcome of this snippet?
<?php
$value = (int) (float) '12.9';
echo $value;
?>Question 35
Why is it valuable to log both the original value and its cast form during complex imports?
