Superglobals · Lesson 3 of 8
$_REQUEST
Understand the combined request input array and why explicit sources are preferable.
Lesson
$_REQUEST may combine GET, POST, and cookie values according to server configuration. That ambiguity makes it difficult to know where a value came from and can allow a cookie to affect logic intended for a form. Prefer $_GET or $_POST explicitly and validate the source you expect.
For state-changing forms, read POST data and use a CSRF token rather than trusting a merged request value.
Further guidance
Superglobal values come from the request or process environment and can be missing or have an unexpected type. Check the key and shape before use, validate it for the operation, and avoid printing secrets or trusting client-controlled headers. Prefer the specific input source over merged request data.
Practice task: Create a fresh example for $_REQUEST. Predict the result before running it, then change one input and explain why the result changes. Test a normal case and one boundary or invalid case where it applies. Compare your output with the example and note one rule you would use in a real project.
Code examples
$_REQUEST
PHP
$search = $_GET['q'] ?? '';
if (! is_string($search)) {
$search = '';
}
echo htmlspecialchars($search, ENT_QUOTES, 'UTF-8');
Expected output
An escaped query parameter
Key points
- Understand the combined request input array and why explicit sources are preferable.
- Treat request and environment values as untrusted input.
- Check the expected type and presence before using a value.
Your learning progress
Finish this lesson
Optional practice
PHP playground
This site does not execute learner-provided PHP code. Connect a secured PHP sandbox service before enabling code execution.
Execution is disabled. No code is sent to a server.