Bird
Raised Fist0
PHPprogramming~10 mins

How a PHP request starts and ends - Visual Walkthrough

Choose your learning style10 modes available
Concept Flow - How a PHP request starts and ends
Client sends HTTP request
↓
Web Server receives request
↓
Web Server passes request to PHP
↓
PHP initializes environment
↓
PHP executes script
↓
PHP sends output to Web Server
↓
Web Server sends response to Client
↓
Request ends
This flow shows how a PHP request starts from the client sending a request and ends when the response is sent back after PHP runs the script.
Execution Sample
PHP
<?php
// Simple PHP script
echo "Hello, world!";
This script outputs 'Hello, world!' when a PHP request runs.
Execution Table
StepActionDetailsOutput/Result
1Client sends HTTP requestBrowser requests PHP pageRequest sent to server
2Web Server receives requestServer accepts HTTP requestRequest ready for PHP
3Web Server passes request to PHPServer invokes PHP interpreterPHP starts processing
4PHP initializes environmentLoads variables, settingsReady to run script
5PHP executes scriptRuns echo statementOutputs 'Hello, world!'
6PHP sends output to Web ServerSends generated HTMLOutput ready to send
7Web Server sends response to ClientSends HTTP response with outputClient receives 'Hello, world!'
8Request endsConnection closes or waits for nextRequest cycle complete
💡 Request ends after server sends response back to client
Variable Tracker
VariableStartAfter Step 4After Step 5Final
Output Buffer"""""Hello, world!""Hello, world!"
Request StateNot startedInitializedScript runningCompleted
Key Moments - 3 Insights
Why does PHP need to initialize environment before running the script?
PHP sets up variables and settings so the script can run correctly, as shown in step 4 of the execution_table.
When does the client actually see the output?
The client sees the output after the web server sends the response, which happens at step 7 in the execution_table.
Does PHP keep running after sending output to the web server?
No, PHP finishes execution and the request ends as shown in step 8 of the execution_table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, at which step does PHP start running the script?
AStep 3
BStep 5
CStep 4
DStep 6
💡 Hint
Check the 'Action' column for when PHP executes the script.
According to variable_tracker, what is the Output Buffer value after step 5?
A"Hello, world!"
B""
CNot initialized
DRequest completed
💡 Hint
Look at the Output Buffer row under 'After Step 5' column.
If the web server never sends the response, which step in execution_table would not happen?
AStep 6
BStep 8
CStep 7
DStep 5
💡 Hint
Step 7 is when the web server sends the response to the client.
Concept Snapshot
PHP Request Flow:
1. Client sends HTTP request
2. Web server receives and passes to PHP
3. PHP initializes environment
4. PHP runs script and generates output
5. Output sent back via web server
6. Client receives response
Request ends after response sent
Full Transcript
A PHP request starts when a client, like a browser, sends an HTTP request to the web server. The server receives this request and passes it to the PHP interpreter. PHP then sets up its environment, preparing variables and settings needed to run the script. Next, PHP executes the script code, for example, running an echo statement to produce output. This output is sent back to the web server, which then sends the HTTP response back to the client. Finally, the request ends, completing the cycle. Variables like the output buffer change from empty to holding the script output during execution. The client only sees the output after the server sends the response. PHP stops running after sending output, marking the end of the request.

Practice

(1/5)
1. What marks the beginning of PHP code in a file?
easy
A. The <php> tag
B. The <script> tag
C. The <?php tag
D. The <?> tag

Solution

  1. Step 1: Identify PHP opening tag

    PHP code always starts with the <?php tag to tell the server to process PHP.
  2. Step 2: Compare with other tags

    Other tags like <script> are for JavaScript, and <php> is invalid.
  3. Final Answer:

    The <?php tag -> Option C
  4. Quick Check:

    PHP starts with <?php [OK]
Hint: PHP code always starts with <?php tag [OK]
Common Mistakes:
  • Confusing PHP tags with HTML or JavaScript tags
  • Using short tags like which may be disabled
  • Thinking PHP starts with ? or other symbols
2. Which of the following correctly ends a PHP block?
easy
A. ?>
B. </php>
C. <?php
D. <?>

Solution

  1. Step 1: Identify PHP closing tag

    The PHP block ends with ?> to signal the end of PHP code.
  2. Step 2: Validate other options

    </php> is invalid HTML, <?php starts PHP, and <?> is not a valid closing tag.
  3. Final Answer:

    ?> -> Option A
  4. Quick Check:

    PHP ends with ?> [OK]
Hint: PHP ends with ?> tag to close code block [OK]
Common Mistakes:
  • Using HTML closing tags like
  • Confusing PHP start and end tags
  • Omitting the closing tag when mixing PHP and HTML
3. What will be the output of this PHP code?
<?php
echo 'Hello';
?>
medium
A. Hello
B. <?php echo 'Hello'; ?>
C. Syntax Error
D. No output

Solution

  1. Step 1: Understand PHP code execution

    The PHP code inside <?php ?> runs on the server and outputs the string 'Hello'.
  2. Step 2: Check output behavior

    The echo statement prints 'Hello' to the page, so the output is exactly that.
  3. Final Answer:

    Hello -> Option A
  4. Quick Check:

    echo 'Hello' outputs Hello [OK]
Hint: echo prints text inside PHP tags to the page [OK]
Common Mistakes:
  • Expecting PHP code to show as text in browser
  • Confusing PHP tags with HTML tags
  • Thinking PHP code causes syntax error without semicolon
4. Identify the error in this PHP snippet:
<?php
echo 'Hi'
?>
medium
A. Incorrect PHP start tag
B. Using single quotes instead of double quotes
C. Missing closing PHP tag
D. Missing semicolon after echo statement

Solution

  1. Step 1: Check PHP syntax rules

    Each PHP statement must end with a semicolon; here, echo 'Hi' lacks it.
  2. Step 2: Validate other parts

    The start tag <?php and closing tag ?> are correct; single quotes are valid in PHP.
  3. Final Answer:

    Missing semicolon after echo statement -> Option D
  4. Quick Check:

    Statements need semicolon [OK]
Hint: Every PHP statement ends with a semicolon [OK]
Common Mistakes:
  • Forgetting semicolon at end of statements
  • Thinking PHP tags are case-sensitive
  • Assuming quotes type causes syntax error
5. Which statement best describes how a PHP request lifecycle works?
hard
A. PHP code runs once when the server starts and stays in memory
B. Each request runs PHP code fresh, then ends after sending response
C. PHP code runs only if the closing tag is omitted
D. PHP code runs continuously until manually stopped

Solution

  1. Step 1: Understand PHP request lifecycle

    PHP runs code fresh on each page request, processes it, then ends after sending the response.
  2. Step 2: Evaluate other options

    PHP does not run continuously or stay in memory; closing tag presence does not control execution.
  3. Final Answer:

    Each request runs PHP code fresh, then ends after sending response -> Option B
  4. Quick Check:

    PHP runs per request, then ends [OK]
Hint: PHP runs fresh on each request, then stops [OK]
Common Mistakes:
  • Thinking PHP code runs persistently like a service
  • Believing PHP runs only if closing tag is missing
  • Assuming PHP code stays loaded between requests
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard