The difference between exit() and die() in PHP_PHP Tutorial
先思考一个问题:
如下代码会向页面显示什么?
代码如下 | |
| |
代码如下 | |
die(123); ?> |
die(123);
?>
曾经有段时间我一直认为 页面会显示 123,但实践结果告诉我,答案错了,页面一片空白!
代码如下 | |
echo '123'; die(); ?> |
代码如下 | |
echo '123'; <🎜> die(); <🎜> ?> |
A piece of information on the Internet:
The difference between exit() and die() in PHP
PHP manual: die()Equivalent to exit().
Note: die() and exit() are both functions to terminate script execution; in fact, the two names exit and die point to the same function, and die() is an alias of the exit() function. This function only accepts one parameter, which can be a value returned by a program or a string, or no parameters can be entered, and the result is no return value.
Reference: Although the two are the same, there are subtle selectivity in their usual use. For example:
When the value passed to the exit and die functions is 0, it means that the execution of the script is terminated early, usually with the name exit().
The code is as follows | |||||
echo "1111";
exit(0);
echo "2222"; |
// 22222 will not be output because when the program reaches exit(0), the script has been terminated early and "will die immediately".
代码如下 | |
$fp=fopen("./readme.txt","r") or die("不能打开该文件"); |
2 // In this case, if the fopen function is called and returns a Boolean value of false, die() will immediately terminate the script and print
immediately3 // The string passed to it, "I can say one or two words before I die".
------------------------------------------------ ----------------------------------
Returning to the previous topic, why doesn’t the following code output 123 to the page?
The code is as follows | |
| |
代码如下 | |
die(123); // 或 exit(123); ?> |
die(123);
// or exit(123);
?>
Summary by yourself:
1. Functionally, die() is equivalent to exit();
2. PHP has multiple running modes, either in website form or in script form (no Web server is required).
When PHP is run in script form, it is recommended to use exit():
For example, the Bash Shell script language, when it wants to stop running, will use the exit() function to terminate the script and allow the output content to the running environment (usually stored in a global variable), but the output content is only Can be a number, indicating the "end status of the command".
In other words, exit(123) only outputs a running status of 123, rather than actually outputting 123 to the console. If you want to output 123 to the console, the code must be changed to the following form:
The code is as follows | |
| |
代码如下 | |
exit('123'); |
exit('123');
?>
When PHP is running as a website, it is recommended to use die():
But at this time, die (number) is meaningless, because it will not output a number string to the page. In other words, if you want the page to terminate and output numbers, you have to change it to the following form
代码如下 | |
die('123'); ?> |
The code is as follows | |
die('123'); <🎜> ?> |

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

C language functions are the basis for code modularization and program building. They consist of declarations (function headers) and definitions (function bodies). C language uses values to pass parameters by default, but external variables can also be modified using address pass. Functions can have or have no return value, and the return value type must be consistent with the declaration. Function naming should be clear and easy to understand, using camel or underscore nomenclature. Follow the single responsibility principle and keep the function simplicity to improve maintainability and readability.

In PHP8, match expressions are a new control structure that returns different results based on the value of the expression. 1) It is similar to a switch statement, but returns a value instead of an execution statement block. 2) The match expression is strictly compared (===), which improves security. 3) It avoids possible break omissions in switch statements and enhances the simplicity and readability of the code.

Although C and C# have similarities, they are completely different: C is a process-oriented, manual memory management, and platform-dependent language used for system programming; C# is an object-oriented, garbage collection, and platform-independent language used for desktop, web application and game development.

Detailed explanation of XPath search method under DOM nodes In JavaScript, we often need to find specific nodes from the DOM tree based on XPath expressions. If you need to...

The... (splat) operator in PHP is used to unpack function parameters and arrays, improving code simplicity and efficiency. 1) Function parameter unpacking: Pass the array element as a parameter to the function. 2) Array unpacking: Unpack an array into another array or as a function parameter.

In PHP, you can effectively prevent CSRF attacks by using unpredictable tokens. Specific methods include: 1. Generate and embed CSRF tokens in the form; 2. Verify the validity of the token when processing the request.

The necessity of registering VueRouter in the index.js file under the router folder When developing Vue applications, you often encounter problems with routing configuration. Special...
