Terminating the execution of a script is usually one of those last ditch ways to handle an error, but it is something we do from time to time, especially if we’re debugging. To terminate the execution of a script, you can call either exit or die, die being an alias of exit.

die('All done!'); // Is not executed since we already ran exit above ;)

Both of these language constructs can be called without an arguments which effectively results in terminating the script without returning an error code (or error code 0). This is what happens normally when the end of a script is reached.

Along with simply terminating the script, you could pass in an error code (an integer between 1 and 254) or a string. When a string is passed in, the value is output before exiting, integer values end up not being displayed so they don’t serve a lot of purpose when trying to debug, but could be sniffed out in other scenarios.

Please note that even when you are terminating a script any shutdown functions you have registered and any object destructors will still be executed.

Did you enjoy this post?

Cool if I slip into your inbox with more?
Full posts, 1-2 times per week: