Is there any disadvantage in using die or exit in php?

Source: Internet
Author: User
Tags exit in
Php uses die or exit in a lot. Is there any other disadvantage in readability? Php is widely used. dieOr exitAre there other drawbacks besides readability?

Reply content:

Php is widely used.dieOrexitAre there other drawbacks besides readability?

I thinkdie()/exit()It mainly affects program design, that is, it affects readability and future maintainability. I am determined to resist brainless use.die()/exit().

Before php introduced the exception mechanism,die()/exit()It is the only way to exit the program in advance. However, with the exception mechanism, you should learn the java Design Philosophy: return the service error with the return value, and throw an exception for the request that cannot be processed. This design ensures that the caller has sufficient control in any case, so that the entire system is robust.

When designing tool classes and tool functions,die()/exit()It should be strictly forbidden because they do not have the right to determine the life and death of the entire program.

When writing a specific business script,die()/exit()It is only used when a status code is returned to the shell or the service is confirmed to have ended. It is best to disable it in other cases.

die()Andexit()They all mean that the end ends naturally here. I think it is normal. Why is there a readability problem? Private think of ultra-long logical nesting and multipleincludeIt is more likely to cause your so-called readability problems.

The exit and die scripts run manually. As the name suggests, there is certainly no readability problem.
In terms of performance, directly end the script operation, which is beneficial to performance.
If you have to talk about the disadvantages, I think it is: you should use exit when you use return, and the script may exit early, resulting in functional problems. However, I think this is a problem for the person who writes the code, and it has nothing to do with the syntax structure.
Therefore, you should use it. You can use it with confidence. It is intended for use.

For program code writing, I agree to accept @ overridedieAndexitYesProgramEnd.

However, for the design of functions and libraries, I think they should not be used,return. Because a function call should not stop the entire program (OMG, this function has too much permission ). Whether the function is called correctly or has encountered a problem, the caller should be notified. Of course, the method is throughreturn.

You have never encountered performance problems, but if you use the trigger_error () function to replace die (), your code will be more advantageous in error handling, it is easier for client programmers to handle errors.
Below is an article I forwarded, you can look: http://blog.csdn.net/yipiankongbai/article/details/17568223

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.