How a PHP request starts and ends - Performance & Efficiency
We want to see how the time a PHP request takes changes as the request does more work.
How does the number of steps grow from start to finish of a PHP request?
Analyze the time complexity of the following PHP request flow.
<?php
// Start of request
session_start();
// Process input
$data = $_GET['data'] ?? '';
$result = processData($data);
// Output result
echo $result;
function processData($input) {
return strtoupper($input);
}
?>
This code starts a session, processes input by converting it to uppercase, and then outputs the result.
Look for loops or repeated steps in the request.
- Primary operation: The call to
strtoupperprocesses each character in the input string. - How many times: Once per character in the input string.
The time to process grows as the input string gets longer because each character is changed to uppercase.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 character changes |
| 100 | About 100 character changes |
| 1000 | About 1000 character changes |
Pattern observation: The work grows directly with the input size.
Time Complexity: O(n)
This means the time to complete the request grows in a straight line as the input gets bigger.
[X] Wrong: "The request time is always the same no matter the input size."
[OK] Correct: The processing step depends on input length, so bigger input means more work and more time.
Understanding how request time grows helps you write efficient code and explain your thinking clearly in interviews.
"What if the processData function used a loop inside to do more work per character? How would the time complexity change?"