Home Backend Development PHP Tutorial The difference between exit() and die() in PHP_PHP Tutorial

The difference between exit() and die() in PHP_PHP Tutorial

Jul 13, 2016 am 10:35 AM
php and What code the difference think show of question page

   先思考一个问题:

  如下代码会向页面显示什么?

 代码如下  

 

代码如下

die(123);

?>

 die(123); 

 ?>

  曾经有段时间我一直认为 页面会显示 123,但实践结果告诉我,答案错了,页面一片空白!

 代码如下  

 

echo '123';

die();

?>
 

  一直不知道为什么,死活不输出123,为了让页面输出123,我把它修改为如下代码:
 代码如下  

 

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";

 代码如下  

 echo "1111"; 

 exit(0); 

 echo "2222"; 

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("不能打开该文件"); 
When the program errors, you can pass it a string, which will be output as it is on the system terminal, usually using the name 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

immediately

 3 // 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'); <🎜>

?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/743706.htmlTechArticleThink about a question first: What will the following code display to the page? The code is as follows? php die(123); ? Once For a while I always thought that the page would display 123, but practice results told me that the answer...
Statement of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

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,

What are the basic requirements for c language functions What are the basic requirements for c language functions Apr 03, 2025 pm 10:06 PM

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.

Explain the match expression (PHP 8 ) and how it differs from switch. Explain the match expression (PHP 8 ) and how it differs from switch. Apr 06, 2025 am 12:03 AM

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.

What are the differences and connections between c and c#? What are the differences and connections between c and c#? Apr 03, 2025 pm 10:36 PM

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.

How to use XPath to search from a specified DOM node in JavaScript? How to use XPath to search from a specified DOM node in JavaScript? Apr 04, 2025 pm 11:15 PM

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...

Describe the purpose and usage of the ... (splat) operator in PHP function arguments and array unpacking. Describe the purpose and usage of the ... (splat) operator in PHP function arguments and array unpacking. Apr 06, 2025 am 12:07 AM

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.

What is Cross-Site Request Forgery (CSRF) and how do you implement CSRF protection in PHP? What is Cross-Site Request Forgery (CSRF) and how do you implement CSRF protection in PHP? Apr 07, 2025 am 12:02 AM

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.

Why do you need to call Vue.use(VueRouter) in the index.js file under the router folder? Why do you need to call Vue.use(VueRouter) in the index.js file under the router folder? Apr 05, 2025 pm 01:03 PM

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...

See all articles