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?"
Practice
Solution
Step 1: Identify PHP opening tag
PHP code always starts with the<?phptag to tell the server to process PHP.Step 2: Compare with other tags
Other tags like<script>are for JavaScript, and<php>is invalid.Final Answer:
The <?php tag -> Option CQuick Check:
PHP starts with <?php [OK]
- Confusing PHP tags with HTML or JavaScript tags
- Using short tags like which may be disabled
- Thinking PHP starts with ? or other symbols
Solution
Step 1: Identify PHP closing tag
The PHP block ends with?>to signal the end of PHP code.Step 2: Validate other options
</php>is invalid HTML,<?phpstarts PHP, and<?>is not a valid closing tag.Final Answer:
?> -> Option AQuick Check:
PHP ends with ?> [OK]
- Using HTML closing tags like
- Confusing PHP start and end tags
- Omitting the closing tag when mixing PHP and HTML
<?php echo 'Hello'; ?>
Solution
Step 1: Understand PHP code execution
The PHP code inside<?php ?>runs on the server and outputs the string 'Hello'.Step 2: Check output behavior
Theechostatement prints 'Hello' to the page, so the output is exactly that.Final Answer:
Hello -> Option AQuick Check:
echo 'Hello' outputs Hello [OK]
- Expecting PHP code to show as text in browser
- Confusing PHP tags with HTML tags
- Thinking PHP code causes syntax error without semicolon
<?php echo 'Hi' ?>
Solution
Step 1: Check PHP syntax rules
Each PHP statement must end with a semicolon; here,echo 'Hi'lacks it.Step 2: Validate other parts
The start tag<?phpand closing tag?>are correct; single quotes are valid in PHP.Final Answer:
Missing semicolon after echo statement -> Option DQuick Check:
Statements need semicolon [OK]
- Forgetting semicolon at end of statements
- Thinking PHP tags are case-sensitive
- Assuming quotes type causes syntax error
Solution
Step 1: Understand PHP request lifecycle
PHP runs code fresh on each page request, processes it, then ends after sending the response.Step 2: Evaluate other options
PHP does not run continuously or stay in memory; closing tag presence does not control execution.Final Answer:
Each request runs PHP code fresh, then ends after sending response -> Option BQuick Check:
PHP runs per request, then ends [OK]
- Thinking PHP code runs persistently like a service
- Believing PHP runs only if closing tag is missing
- Assuming PHP code stays loaded between requests
Ready to prove it?
Pass the test and this pattern moves unknown
