PHP process model per request - Time & Space Complexity
When PHP handles a web request, it starts fresh each time. This means the work done depends on what the request asks for.
We want to see how the time PHP takes grows as the request does more work.
Analyze the time complexity of the following PHP script handling a request.
<?php
// Simulate processing a list of items per request
$items = range(1, $n); // $n is input size
foreach ($items as $item) {
// Process each item
echo $item . "\n";
}
?>
This code processes each item in a list one by one for each request.
Look for loops or repeated steps.
- Primary operation: The foreach loop that goes through each item.
- How many times: Exactly once for each item in the list, so $n times.
As the number of items grows, the work grows too.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 loops, 10 prints |
| 100 | 100 loops, 100 prints |
| 1000 | 1000 loops, 1000 prints |
Pattern observation: The work grows directly with the number of items. Double the items, double the work.
Time Complexity: O(n)
This means the time PHP takes grows in a straight line with the number of items it processes.
[X] Wrong: "PHP keeps data between requests, so processing time stays the same no matter the input size."
[OK] Correct: PHP starts fresh for each request, so it must do all the work again. More items mean more work each time.
Understanding how PHP handles each request helps you explain how your code scales and why efficient loops matter in real projects.
"What if we added a nested loop inside the foreach to process pairs of items? How would the time complexity change?"
Practice
Solution
Step 1: Understand PHP request lifecycle
PHP starts fresh for each request, so variables exist only during that request.Step 2: Identify variable persistence
Variables reset after request ends unless saved in sessions or databases.Final Answer:
They are reset and lost unless saved externally -> Option DQuick Check:
PHP variables reset each request = D [OK]
- Thinking variables persist automatically between requests
- Confusing PHP with long-running server processes
- Assuming global variables keep values across users
Solution
Step 1: Analyze variable initialization
In a new request, variables start fresh; $count = 0 sets it properly.Step 2: Check other options for persistence
Options A, B, D try to increment without initialization or rely on static/session which need extra setup.Final Answer:
<?php $count = 0; echo $count; ?> -> Option CQuick Check:
New request means fresh variables = C [OK]
- Assuming static variables keep value across requests
- Using session without starting it
- Incrementing uninitialized variables
<?php
session_start();
if (!isset($_SESSION['visits'])) {
$_SESSION['visits'] = 1;
} else {
$_SESSION['visits']++;
}
echo $_SESSION['visits'];
?>Solution
Step 1: Understand session persistence
Session data keeps values across requests, so visits count increases.Step 2: Trace two requests
First request sets visits=1; second request increments to 2.Final Answer:
1 then 2 -> Option AQuick Check:
Session stores visits count = 1 then 2 [OK]
- Thinking variables persist without sessions
- Forgetting to call session_start()
- Expecting static variables to persist across requests
<?php $count = 0; $count++; echo $count; ?>
Solution
Step 1: Check variable behavior per request
$count is set to 0 every time, so increment always starts fresh.Step 2: Identify persistence issue
Without sessions or external storage, counter resets each request.Final Answer:
Variables reset each request, so counter never increases -> Option AQuick Check:
Variables reset each request = A [OK]
- Assuming static variables persist across requests
- Confusing syntax errors with logic errors
- Forgetting to start sessions for persistence
Solution
Step 1: Understand PHP process model limits
PHP variables reset each request, so they can't keep visitor count.Step 2: Identify persistent storage method
Database stores data permanently and can be updated each request.Final Answer:
Store visitor count in a database and increment on each request -> Option BQuick Check:
Persistent storage needed for visitor count = A [OK]
- Using static or normal variables expecting persistence
- Ignoring need for external storage
- Confusing sessions with static variables
Ready to prove it?
Pass the test and this pattern moves unknown
