PHP Input Handling Quiz
A 30-question quiz on PHP input handling that balances code review scenarios and short snippets covering validation, sanitization, filtering, and consistent error handling for web and CLI inputs.
Question 1
Why does guidance often insist on validating inputs at the perimeter of a controller rather than inside deep utility classes?
Question 2
What does this snippet echo when /api.php?id=42?
<?php
$id = filter_input(INPUT_GET, 'id', FILTER_VALIDATE_INT);
echo $id === false ? 'invalid' : $id;
?>Question 3
Why should scripts treat filter_input returning null differently from false?
Question 4
What does this POST handler echo when the email field is absent?
<?php
$email = filter_input(INPUT_POST, 'email', FILTER_VALIDATE_EMAIL);
echo $email === null ? 'missing' : ($email === false ? 'invalid' : 'ok');
?>Question 5
Why do teams prefer hash_equals when comparing API tokens from $_SERVER["HTTP_X_TOKEN"]?
Question 6
What does this CSRF token check echo?
<?php
$token = $_POST['csrf'] ?? '';
echo hash_equals($_SESSION['csrf'] ?? '', $token) ? 'pass' : 'fail';
?>Question 7
Why do review guidelines prefer filter_input(INPUT_SERVER, "HTTP_X_FORWARDED_PROTO") over direct $_SERVER checks when proxies are involved?
Question 8
What does this request-method gate echo when the client sends POST?
<?php
echo $_SERVER['REQUEST_METHOD'] === 'POST' ? 'process' : 'ignore';
?>Question 9
Why is json_decode($body, true, 512, JSON_THROW_ON_ERROR) considered safer than silent decoding when handling JSON payloads?
Question 10
What does this JSON handler echo when the payload is {"name":"Ops"}?
<?php
$data = json_decode(file_get_contents('php://input'), true, 512, JSON_THROW_ON_ERROR);
echo $data['name'] ?? 'missing';
?>Question 11
Why do form-handling controllers typically trim and normalize whitespace before validation?
Question 12
What does this sanitization snippet echo?
<?php
$raw = " [email protected] ";
$clean = filter_var(trim($raw), FILTER_VALIDATE_EMAIL);
echo $clean ?: 'invalid';
?>Question 13
Why do payment endpoints often reject inputs containing control characters even if they pass regex validation?
Question 14
What does this control-character filter output?
<?php
$input = "order 123";
if (preg_match('/[ -]/', $input)) {
echo 'reject';
} else {
echo 'accept';
}
?>Question 15
Why is it recommended to log raw inputs separately from sanitized values when debugging?
Question 16
What does this raw-vs-sanitized logger echo?
<?php
$raw = $_GET['sku'] ?? '';
$clean = preg_replace('/[^A-Za-z0-9_-]/', '', $raw);
echo $raw . ' -> ' . $clean;
?>Question 17
Why is filter_var with FILTER_VALIDATE_BOOL and FILTER_NULL_ON_FAILURE handy for checkbox inputs?
Question 18
What does this boolean filter echo when $_POST["active"]="on"?
<?php
$active = filter_var($_POST['active'] ?? null, FILTER_VALIDATE_BOOL, FILTER_NULL_ON_FAILURE);
echo $active === null ? 'invalid' : ($active ? 'true' : 'false');
?>Question 19
Why should CLI scripts validate $argv length before accessing arguments?
Question 20
What does this CLI validator echo when no argument is provided?
<?php
if ($argc < 2) {
echo "usage: php script.php <name>";
exit;
}
echo $argv[1];
?>Question 21
Why is it useful to centralize input parsing in a Request object or helper class?
Question 22
What does this Request helper usage echo?
<?php
class Request {
public function input(string $key, $default = null) {
return $_POST[$key] ?? $_GET[$key] ?? $default;
}
}
$request = new Request();
echo $request->input('q', 'none');
?>Question 23
Why should file upload handlers enforce a maximum size before calling move_uploaded_file?
Question 24
What does this size check echo when an uploaded file is 5 MB and the limit is 2 MB?
<?php
$file = $_FILES['doc'] ?? null;
$limit = 2 * 1024 * 1024;
if (!$file || $file['error'] !== UPLOAD_ERR_OK) {
echo 'error';
} elseif ($file['size'] > $limit) {
echo 'too large';
} else {
echo 'ok';
}
?>Question 25
Why should CLI scripts reading STDIN check stream metadata before assuming the input is textual?
Question 26
What does this STDIN reader echo when input is "hello"?
<?php
$stdin = trim(stream_get_contents(STDIN));
echo $stdin ?: 'empty';
?>Question 27
Why do APIs respond with HTTP 422 or 400 when required input fields are missing?
Question 28
What does this response builder echo when required fields are absent?
<?php
$payload = json_decode(file_get_contents('php://input'), true);
if (empty($payload['name'])) {
http_response_code(422);
echo 'missing name';
exit;
}
echo 'ok';
?>Question 29
Why should controllers log invalid input payloads with correlation IDs rather than plain text messages?
Question 30
What does this logging helper echo when it captures a bad payload?
<?php
$correlationId = $_SERVER['HTTP_X_REQUEST_ID'] ?? uniqid('req_', true);
$payload = $_POST ?: [];
echo $correlationId . ':' . json_encode($payload);
?>