Home >Backend Development >PHP Problem >How to use the exit() function in PHP

How to use the exit() function in PHP

autoload
autoloadOriginal
2021-04-14 15:27:412579browse

How to use the exit() function in PHP

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 = &#39;./exit.html&#39;;
    $file = @fopen($filename, &#39;r&#39;)
    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 = &#39;./exit.html&#39;;
echo "此处使用了exit()";

$file = @fopen($filename, &#39;r&#39;)
    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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn