$_REQUEST behavior in PHP - Time & Space Complexity
When using $_REQUEST in PHP, it's important to understand how accessing this superglobal affects performance.
We want to know how the time to get data from $_REQUEST grows as the input size changes.
Analyze the time complexity of the following code snippet.
// Accessing multiple keys from $_REQUEST
foreach ($keys as $key) {
$value = $_REQUEST[$key] ?? null;
// process $value
}
This code loops through a list of keys and fetches each corresponding value from $_REQUEST.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping through each key to access
$_REQUESTvalues. - How many times: Once for each key in the
$keysarray.
Each additional key means one more access to $_REQUEST. So, the work grows directly with the number of keys.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 accesses to $_REQUEST |
| 100 | 100 accesses to $_REQUEST |
| 1000 | 1000 accesses to $_REQUEST |
Pattern observation: The number of operations increases evenly as the input size grows.
Time Complexity: O(n)
This means the time to access values from $_REQUEST grows in direct proportion to how many keys you look up.
[X] Wrong: "Accessing $_REQUEST is always instant and does not depend on input size."
[OK] Correct: While each access is fast, doing many accesses adds up, so total time depends on how many keys you check.
Understanding how superglobals like $_REQUEST behave helps you write efficient PHP code and shows you can think about performance in real projects.
"What if we accessed $_POST directly instead of $_REQUEST? How would the time complexity change?"
Practice
$_REQUEST contain?Solution
Step 1: Understand the purpose of
$_REQUEST$_REQUESTis designed to collect data from multiple sources: GET, POST, and COOKIE.Step 2: Identify what
It merges these three superglobals into one array for easier access.$_REQUESTincludesFinal Answer:
Data from GET, POST, and COOKIE combined -> Option AQuick Check:
$_REQUEST= GET + POST + COOKIE [OK]
- Thinking $_REQUEST only contains GET or POST data
- Confusing $_REQUEST with $_POST or $_GET
- Assuming $_REQUEST excludes cookies
username using $_REQUEST in PHP?Solution
Step 1: Recall PHP array syntax
PHP superglobals like$_REQUESTare associative arrays accessed with square brackets and quotes for keys.Step 2: Identify correct syntax for accessing 'username'
The correct syntax is$_REQUEST['username']. Using arrows or dots is invalid for arrays.Final Answer:
$_REQUEST['username'] -> Option DQuick Check:
Array access uses brackets and quotes [OK]
- Using object notation (->) instead of array syntax
- Using dot notation like in JavaScript
- Forgetting quotes around the key
script.php?color=red and the form sends color=blue via POST?<?php print_r($_REQUEST['color']); ?>
Solution
Step 1: Understand the order of variables in $_REQUEST
By default, PHP's$_REQUESTuses the order: GET, POST, COOKIE. POST data overrides GET if keys are the same.Step 2: Determine which value
Since POST sends$_REQUEST['color']holdscolor=blue, it overrides the GETcolor=red. So,$_REQUEST['color']is 'blue'.Final Answer:
blue -> Option CQuick Check:
POST overrides GET in $_REQUEST [OK]
- Assuming GET value appears first
- Confusing $_REQUEST with $_GET or $_POST
- Expecting an array output
<?php print_r($_REQUEST['user']); ?>
If the form sends no 'user' field and the URL has no 'user' parameter, what error will occur and how to fix it?
Solution
Step 1: Identify the error from accessing missing key
Accessing$_REQUEST['user']when 'user' is not set causes a Notice: Undefined index.Step 2: Fix by checking if key exists
Useisset($_REQUEST['user'])before accessing to avoid the notice.Final Answer:
Notice: Undefined index; fix by checking with isset() -> Option BQuick Check:
Check keys with isset() to avoid notices [OK]
- Ignoring undefined index notices
- Confusing undefined index with undefined variable
- Assuming no error occurs when key missing
$_REQUEST, but prefer POST over GET and GET over COOKIE. Which code snippet correctly respects this priority?Solution
Step 1: Understand priority order requirement
The priority is POST > GET > COOKIE, so check in that order.Step 2: Analyze each option's order
if(isset($_POST['lang'])) { $lang = $_POST['lang']; } elseif(isset($_GET['lang'])) { $lang = $_GET['lang']; } elseif(isset($_COOKIE['lang'])) { $lang = $_COOKIE['lang']; } else { $lang = 'en'; } checks POST first, then GET, then COOKIE, assigning default 'en' if none set. $lang = $_REQUEST['lang'] ?? 'en'; uses $_REQUEST which merges but order depends on php.ini and is less explicit. Options C and D check in wrong orders.Final Answer:
Option A code snippet -> Option AQuick Check:
Explicit checks respect priority best [OK]
- Relying on $_REQUEST without knowing order
- Checking keys in wrong priority order
- Not providing a default value
Ready to prove it?
Pass the test and this pattern moves unknown
