0
0
PHPprogramming~20 mins

How a PHP request starts and ends - Practice Exercises

Choose your learning style9 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.