RDFrame Posted June 10, 2006 Share Posted June 10, 2006 Is there any way in PHP to run some code at the end of every execution? I don't mean just placing the code at the bottom of the script, because that won't work with this. For example, in Perl there's the BEGIN { } and END { } constructs. Every time that Perl program finishes execution, whatever code is between END { } will be ran.For example, if exit() is called somewhere in the PHP program, I still want a small block of code to be executed. Is there any way to do this in PHP? I looked through the manual, but couldn't find anything. Or is this not supported by PHP?Thanks,Matt Quote Link to comment https://forums.phpfreaks.com/topic/11651-run-code-at-end-of-execution/ Share on other sites More sharing options...
Ninjakreborn Posted June 10, 2006 Share Posted June 10, 2006 a few possibilities.Try and put the exit function in side the bracket you want to close, and then start up a nother one, or you could try sleep, and unsleep instead, but I think that will be a different affect. The only other idea I had was, like people told me on here maybe you could keep a variable and start it as true at the top, and then don't use the variable at all then after the kill statemtn, you could say if whatever is true, then continue your code, but then again if kill ends the script I don't see how it will make it to the end. You could use or dieI don't know if sql will work though when randomly thrown in throughout php, but it's worth a thought. Quote Link to comment https://forums.phpfreaks.com/topic/11651-run-code-at-end-of-execution/#findComment-44005 Share on other sites More sharing options...
wildteen88 Posted June 10, 2006 Share Posted June 10, 2006 You can place PHP code within the parentheses inside the exit function, or you can create a function which contains the code that you want to be run you call the exit function like so:[code]function exitFunc() { // do something here, when you exit the script}exit(exitFunc());[/code]Does that help? Quote Link to comment https://forums.phpfreaks.com/topic/11651-run-code-at-end-of-execution/#findComment-44011 Share on other sites More sharing options...
RDFrame Posted June 10, 2006 Author Share Posted June 10, 2006 Alright, so it's not supported by PHP. I guess that's why I couldn't find it in the manual. :DThanks for your time guys. I'll figure something else out.Matt Quote Link to comment https://forums.phpfreaks.com/topic/11651-run-code-at-end-of-execution/#findComment-44015 Share on other sites More sharing options...
RDFrame Posted June 10, 2006 Author Share Posted June 10, 2006 Nevermind, got it solved from the boys over at SitePoint. If anyone else ever needs something like this, check out the register_shutdown_function() in PHP. It's exactly what I was looking for. :)Matt Quote Link to comment https://forums.phpfreaks.com/topic/11651-run-code-at-end-of-execution/#findComment-44031 Share on other sites More sharing options...
wildteen88 Posted June 10, 2006 Share Posted June 10, 2006 I was actully going to mention that earlier, but I didn't. Quote Link to comment https://forums.phpfreaks.com/topic/11651-run-code-at-end-of-execution/#findComment-44069 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.