Jump to content

Archived

This topic is now archived and is closed to further replies.

RDFrame

Run code at end of execution?

Recommended Posts


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

Share this post


Link to post
Share on other sites
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 die
I don't know if sql will work though when randomly thrown in throughout php, but it's worth a thought.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites

Alright, so it's not supported by PHP. I guess that's why I couldn't find it in the manual. :D

Thanks for your time guys. I'll figure something else out.

Matt

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites
I was actully going to mention that earlier, but I didn't.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.