How is the exit function used in PHP?

In PHP, the exit function is used to terminate the execution of the current script and optionally output a message. The syntax is as follows:

exit(string $message)

The parameter $message is optional and is used to specify the message that will be output before terminating the script. If the $message parameter is not provided, the script will terminate immediately.

One option is to use the exit function during script execution to forcefully terminate the script based on certain conditions, while also displaying a message to the user or developer for debugging or providing a prompt. For example:

$age = 17;

if ($age < 18) {
    exit("You must be at least 18 years old to access this page.");

// 继续执行代码

In the example above, a message will be displayed and the script will stop executing if $age is less than 18.

Leave a Reply 0

Your email address will not be published. Required fields are marked *