exit()
The function outputs a message and exits the current script. This function is an alias of the die()
function.
Syntax:
exit ( string $status )
exit ( int $status )
If $status is a string, the function will print $status before exiting .
If $status is an int, the value will be used as the exit status code and will not be printed. Exit status codes should be in the range 0 to 254, and the exit status code 255, which is reserved by PHP, should not be used. Status code 0 is used to terminate the program successfully.
1. If the parameter is a string:
<?php $filename = './exit.html'; $file = @fopen($filename, 'r') or exit("打开文件($filename)失败"); ?>
Output: Open file (./exit .html
) failed because the file exit.html
does not exist in the current path.
2. If the parameter is empty:
<?php $filename = './exit.html'; echo "此处使用了exit()"; $file = @fopen($filename, 'r') or exit(); ?>
Output: exit() is used here
3. Only contains status code
<?php exit(1); exit(0376); ?>
Recommendation:《2021 PHP interview questions summary (collection) 》《php video tutorial》
The above is the detailed content of How to use the exit() function in PHP. For more information, please follow other related articles on the PHP Chinese website!