Skip to content
Advertisement

The shutdown function in the main php script is not executing when I call exit() from included script

I have a main.php script which include another script named inc.php. When I call exit function from the inc.php, the main.php dies without executing the function registered as a shutdown function. Like:

main.php

<?php
echo "An";
include 'inc.php';
function whenEnd(){
    echo " this won't be echoed ";
}
register_shutdown_function ('whenEnd');
?>

inc.php

<?php
echo "Bn";
exit;
?>

running main.php will return

A
B

But I was expecting

A
B
this won't be echoed

what is wrong?

Advertisement

Answer

The function must be registered before exit.

<?php
echo "An";
function final(){
echo " this won't be echoed ";
}
register_shutdown_function ('final');
include 'inc.php';
?>
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement