Bird
Raised Fist0
PHPprogramming~20 mins

How a PHP request starts and ends - Practice Exercises

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
PHP Request Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this PHP script?
Consider this PHP code that runs during a request. What will it output?
PHP
<?php
register_shutdown_function(function() {
    echo "End of request.";
});
echo "Start of request.";
?>
AEnd of request.Start of request.
BStart of request.End of request.
CStart of request.
DEnd of request.
Attempts:
2 left
💡 Hint
Think about when the shutdown function runs in relation to the main script.
🧠 Conceptual
intermediate
1:30remaining
What happens first when a PHP request starts?
When a PHP request begins, which of these happens first?
APHP parses and compiles the script into opcodes
BThe session is destroyed
CThe shutdown functions are called
DThe script outputs HTML to the browser
Attempts:
2 left
💡 Hint
Think about what PHP must do before running your code.
❓ Predict Output
advanced
2:00remaining
What error does this PHP code produce?
This PHP script tries to use a variable after the request ends. What error will it produce?
PHP
<?php
register_shutdown_function(function() use (&$var) {
    echo $var;
});
$var = "Hello";
?>
AFatal error: Cannot use variable in shutdown function
BUndefined variable: var
CHello
DNo output
Attempts:
2 left
💡 Hint
Variables used with 'use' in shutdown functions keep their value.
🧠 Conceptual
advanced
1:30remaining
Which PHP event marks the end of a request?
Which of these best describes when a PHP request ends?
AWhen the PHP interpreter starts parsing the script
BWhen the server receives the HTTP request
CWhen the user closes the browser tab
DWhen the script finishes execution and all output is sent
Attempts:
2 left
💡 Hint
Think about what happens after your PHP code runs.
🔧 Debug
expert
2:30remaining
Why does this PHP script output only 'Start' and not 'End'?
This PHP script tries to print 'End' at the end of the request but it doesn't appear. Why?
PHP
<?php
register_shutdown_function(function() {
    echo "End";
});
exit("Start");
?>
ABecause exit() outputs 'Start' and then shutdown functions run normally
BBecause echo inside shutdown functions is ignored
CBecause exit() stops the script immediately and shutdown functions are not called
DBecause shutdown functions run before exit()
Attempts:
2 left
💡 Hint
Think about how exit() works with shutdown functions.

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