Script execution and memory reset in PHP - Time & Space Complexity
When a PHP script runs, it starts fresh each time. This means memory and variables reset with every run.
We want to understand how this reset affects the time it takes to run the script as input grows.
Analyze the time complexity of the following code snippet.
<?php
function processData(array $data) {
$sum = 0;
foreach ($data as $value) {
$sum += $value;
}
return $sum;
}
$input = range(1, 1000);
echo processData($input);
?>
This code sums all numbers in an array each time the script runs, starting fresh every time.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping through the array to add each number.
- How many times: Once per script run, exactly as many times as there are items in the array.
As the array gets bigger, the loop runs more times, so the work grows with the number of items.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 additions |
| 100 | 100 additions |
| 1000 | 1000 additions |
Pattern observation: The work grows directly with the input size; double the input, double the work.
Time Complexity: O(n)
This means the time to run the script grows in a straight line with the size of the input array.
[X] Wrong: "Since the script resets memory each time, the time to run stays the same no matter the input size."
[OK] Correct: Even though memory resets, the script still has to do work for every item in the input each run, so bigger input means more work and more time.
Understanding how script execution resets memory helps you explain how PHP handles each request fresh, which is key for writing efficient code that scales well.
"What if we changed the function to call itself recursively instead of using a loop? How would the time complexity change?"
Practice
Solution
Step 1: Understand PHP script execution
PHP runs scripts from top to bottom each time the page loads.Step 2: Memory and variable reset after execution
After the script finishes, all variables and memory used are cleared to keep the environment clean.Final Answer:
All variables and memory are reset and cleared -> Option CQuick Check:
PHP resets variables after script ends = C [OK]
- Thinking variables keep values between page loads
- Assuming variables save to the script file
- Believing variables are shared across scripts automatically
Solution
Step 1: Identify correct PHP tags
PHP scripts start with to run properly.Step 2: Understand script resets
Each time the script runs, variables reset automatically without extra commands.Final Answer:
-> Option AQuick Check:
Correct PHP tags = A [OK]
- Using <script> tags which are for JavaScript
- Trying to call reset() function which doesn't reset variables
- Using incorrect <php> tags
<?php $counter = 0; $counter++; echo $counter; ?>
What will be the output each time the script runs?
Solution
Step 1: Analyze variable initialization
Each time the script runs, $counter is set to 0 at the start.Step 2: Increment and output
$counter++ increases $counter to 1, then echo prints 1 each time.Final Answer:
1 then 1 -> Option AQuick Check:
Variables reset each run, so output is 1 each time [OK]
- Assuming $counter keeps incrementing across runs
- Confusing pre-increment and post-increment
- Thinking echo prints 0 first
<?php $sum = 10; $sum += 5; ?>
But after refreshing the page, $sum is always 15. You want $sum to keep increasing by 5 each time the page loads. What is the problem?
Solution
Step 1: Understand variable reset behavior
PHP clears all variables after each script finishes, so $sum resets to 10 every time.Step 2: Why $sum does not keep increasing
Because $sum is reinitialized to 10 on each page load, adding 5 results in 15 always.Final Answer:
PHP resets variables after each script run, so $sum starts at 10 every time -> Option DQuick Check:
Variables reset each run = D [OK]
- Thinking += operator is faulty
- Assuming global declaration keeps value between runs
- Believing loops affect variable persistence
Solution
Step 1: Recognize PHP variable reset limitation
PHP variables reset after each script run, so count cannot persist in normal variables.Step 2: Use persistent storage
Sessions, files, or databases store data between page loads to keep count.Final Answer:
Store the count in a session or a file/database to persist data -> Option BQuick Check:
Persistent storage needed for count across runs = B [OK]
- Thinking global variables keep values between runs
- Trying to use loops for persistence
- Resetting variables each time defeats counting
Ready to prove it?
Pass the test and this pattern moves unknown
