0
0
PHPprogramming~5 mins

How a PHP request starts and ends - Performance & Efficiency

Choose your learning style9 modes available
Time Complexity: How a PHP request starts and ends
O(n)
Understanding Time Complexity

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?

Scenario Under Consideration

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.

Identify Repeating Operations

Look for loops or repeated steps in the request.

  • Primary operation: The call to strtoupper processes each character in the input string.
  • How many times: Once per character in the input string.
How Execution Grows With Input

The time to process grows as the input string gets longer because each character is changed to uppercase.

Input Size (n)Approx. Operations
10About 10 character changes
100About 100 character changes
1000About 1000 character changes

Pattern observation: The work grows directly with the input size.

Final Time Complexity

Time Complexity: O(n)

This means the time to complete the request grows in a straight line as the input gets bigger.

Common Mistake

[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.

Interview Connect

Understanding how request time grows helps you write efficient code and explain your thinking clearly in interviews.

Self-Check

"What if the processData function used a loop inside to do more work per character? How would the time complexity change?"