What is the benefit of using exit (0) in PHP to declare the exit status (successful exit or unexpected termination of a situation)? What's the difference between this and a direct exit?

Source: Internet
Author: User
Just like
The following code

echo '配置错误';exit(3);  //状态3表示由于配置错误而退出

And

// 直接退出exit('配置错误');

What's the difference?

Humbly ask the great God one or two

Reply content:

Just like
The following code

echo '配置错误';exit(3);  //状态3表示由于配置错误而退出

And

// 直接退出exit('配置错误');

What's the difference?

Humbly ask the great God one or two

Give the conclusion first: there are subtle differences.

To speak the truth or to take a document:

If status is a string, this function prints the status just before exiting.

If status is a integer, that value would be used as the exit status and not printed. Exit statuses should be in the range 0 to 254, the exit status 255 are reserved by PHP and shall not being used. The status 0 is used to terminate the program successfully.
note:php >= 4.2.0 does not print the status if it's an integer.

Simply put: If it is a string, it will print out, if it is a number, it will be the exit status code , will not be printed.


  
   

Executing the two lines of code separately, it is clear that the result is the same.

The difference is:
Comment on the second line, execute the following command at the terminal:
PHP test.php//print "error"
echo $? Printing 3

Comment on the first line, execute the following command at the terminal:
PHP test.php//print "error"
echo $? Printing 0

That is, Eixt () is used as an exit status code when the argument is of type int.
$? Explanation: Stores The exit value of the last command is executed (the exit state of the final one, 0 means no error).

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.