PHP Superglobals Quiz
A 30-question PHP quiz on superglobals, balancing code walkthroughs and review scenarios about $_GET, $_POST, $_SERVER, $_COOKIE, $_SESSION, $_FILES, $_ENV, and $GLOBALS usage.
Question 1
What is the primary reason reviewers warn against accessing $_GET values directly inside deeply nested logic?
Question 2
What does this snippet echo when visiting /page.php?name=Nova?
<?php
echo htmlspecialchars($_GET['name'] ?? 'Guest', ENT_QUOTES, 'UTF-8');
?>Question 3
Why do security guides discourage merging $_GET and $_POST via $_REQUEST for sensitive endpoints?
Question 4
What does this POST-handling snippet echo when $_POST["email"] is missing?
<?php
$email = filter_input(INPUT_POST, 'email', FILTER_VALIDATE_EMAIL);
echo $email === null ? 'no field' : ($email === false ? 'invalid' : 'ok');
?>Question 5
Why should $_SERVER["HTTP_REFERER"] never be used directly for authorization decisions?
Question 6
What will this script echo if the request method is POST?
<?php
echo $_SERVER['REQUEST_METHOD'] === 'POST' ? 'process' : 'skip';
?>Question 7
Why does $_FILES require checking both error codes and mime type rather than trusting user-provided filenames?
Question 8
What does this upload check output if $_FILES["avatar"]["error"] equals UPLOAD_ERR_NO_FILE?
<?php
$file = $_FILES['avatar'] ?? null;
if (!$file || $file['error'] === UPLOAD_ERR_NO_FILE) {
echo 'missing';
} else {
echo 'got file';
}
?>Question 9
Why should $_SESSION writes happen before output buffering is flushed?
Question 10
What does this session snippet echo on first load?
<?php
session_start();
if (!isset($_SESSION['visits'])) {
$_SESSION['visits'] = 0;
}
$_SESSION['visits']++;
echo $_SESSION['visits'];
?>Question 11
Why do hardening guides discourage writing arbitrary keys into $GLOBALS?
Question 12
What does this $GLOBALS access echo?
<?php
$token = 'abc';
function showToken() {
echo $GLOBALS['token'] ?? 'none';
}
showToken();
?>Question 13
Why is putenv/getenv usage typically wrapped in helper functions instead of scattered throughout the app?
Question 14
What does this getenv snippet echo if MY_FLAG is missing?
<?php
$flag = getenv('MY_FLAG');
echo $flag === false ? 'unset' : $flag;
?>Question 15
Why should developers use filter_input instead of raw $_GET/$_POST when sanitizing numerical query parameters?
Question 16
What does this CLI-focused script echo when run as php task.php foo bar?
<?php
echo $_SERVER['argv'][1] ?? 'none';
?>Question 17
Why do documentation templates remind developers to check $_SERVER["HTTPS"] or $_SERVER["REQUEST_SCHEME"] carefully before enforcing redirects?
Question 18
What does this HTTPS check echo on a secure request with $_SERVER["HTTPS"]="on"?
<?php
$isSecure = !empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off';
echo $isSecure ? 'secure' : 'plain';
?>Question 19
Why does $_COOKIE access typically pair with isset checks even if default values are provided elsewhere?
Question 20
What does this cookie snippet echo if "lang" is not set?
<?php
$lang = $_COOKIE['lang'] ?? 'en';
echo $lang;
?>Question 21
Why is it unsafe to trust $_SERVER["REMOTE_ADDR"] for rate limiting behind reverse proxies?
Question 22
What does this snippet echo when REMOTE_ADDR is 192.0.2.1?
<?php
$ip = $_SERVER['REMOTE_ADDR'] ?? '0.0.0.0';
echo $ip;
?>Question 23
Why should $_FILES["file"]["tmp_name"] be passed to move_uploaded_file rather than copying the original name?
Question 24
What does this file-move snippet echo when the upload succeeds?
<?php
$file = $_FILES['avatar'] ?? null;
if ($file && $file['error'] === UPLOAD_ERR_OK) {
echo move_uploaded_file($file['tmp_name'], __DIR__ . '/uploads/' . basename($file['name'])) ? 'saved' : 'failed';
} else {
echo 'invalid';
}
?>Question 25
Why should $_SERVER["HTTP_HOST"] be sanitized before embedding into redirects or HTML titles?
Question 26
What does this host validation snippet echo for a trusted host list ["app.test","admin.test"] if HTTP_HOST is admin.test?
<?php
$allowed = ['app.test', 'admin.test'];
$host = $_SERVER['HTTP_HOST'] ?? '';
echo in_array($host, $allowed, true) ? 'trusted' : 'reject';
?>Question 27
Why is $_ENV typically populated only when variables_order includes "E" in php.ini?
Question 28
What does this script echo if variables_order omits "E" and $_ENV is empty while getenv works?
<?php
$env = $_ENV['MODE'] ?? getenv('MODE') ?? 'default';
echo $env;
?>Question 29
Why do tests often seed $_SERVER, $_GET, and $_POST manually before including controller files?
Question 30
What does this test helper echo when $_POST["token"]="abc", $_SERVER["REMOTE_ADDR"]="127.0.0.1", and $_SERVER["HTTP_X_FORWARDED_FOR"] is unset?
<?php
$_POST['token'] = 'abc';
$_SERVER['REMOTE_ADDR'] = '127.0.0.1';
unset($_SERVER['HTTP_X_FORWARDED_FOR']);
$token = $_POST['token'] ?? '';
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'] ?? $_SERVER['REMOTE_ADDR'] ?? '0.0.0.0';
echo $token . '@' . $ip;
?>