In this chapter, we will look at functions that allow deep control over script execution and communication with the client (browser). These functions are crucial when we want to safely terminate a script or when we need to process raw binary data.
| Function | Description | Example |
|---|---|---|
die() / exit() |
Outputs a message and terminates the execution of the current script. | die("End"); |
connection_status() |
Returns the bitwise status of the current connection with the browser. | 0 = Normal |
connection_aborted() |
Checks whether the user closed the window or aborted the loading. | Returns true/false |
ignore_user_abort() |
Sets whether a script should continue executing even if the user closes the window. | Useful for long processes |
pack() / unpack() |
Pack and unpack binary data string. | For working with bytes |
These two functions are practically identical. We use them when continuing the code makes no sense – for example,
if we cannot connect to the database or if the user does not have permissions to view the page. Everything below
die() is completely ignored by the server.
Did you know that a PHP script usually stops working the moment the user clicks the "X" in the browser or closes
the tab? With the ignore_user_abort(true) function, we can command the server to execute the script to
the end (e.g., to safely write data to the database), regardless of what the user does.