Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I execute remaining php code after calling "exit"?

How can a client send a request to a server just to trigger an execution, where the server immediately sends a response then executes the requested code? Similar to this:

echo 'the execution is starting';
exit;
execution_function(); // <--- is it possible to this function being executed after exit 

I want the response to be immediate so the client can do other things while the server executes the request.

like image 634
stack underflow Avatar asked Jul 15 '12 04:07

stack underflow


People also ask

What does exit () do in PHP?

The exit() function in PHP is an inbuilt function which is used to output a message and terminate the current script. The exit() function only terminates the execution of the script. The shutdown functions and object destructors will always be executed even if exit() function is called.

Does Return exit the function PHP?

The return keyword ends a function and, optionally, uses the result of an expression as the return value of the function. If return is used outside of a function, it stops PHP code in the file from running.

Where is PHP code actually executed?

PHP code is executed on the server.


2 Answers

You can use register_shutdown_function() to set a callback function which will be executed when PHP exits.

like image 149
Michael Hampton Avatar answered Sep 24 '22 05:09

Michael Hampton


You can use register_shutdown_function() after exit() , die() , even you can register fatal error to your error register log using this function.Quite handy

like image 27
munna ss Avatar answered Sep 24 '22 05:09

munna ss