Php uses die or exit in a lot. Is there any other disadvantage in readability? Php is widely used.
die
Or
exit
Are there other drawbacks besides readability?
Reply content:
Php is widely used.die
Orexit
Are 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 multipleinclude
It 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 @ overridedie
Andexit
YesProgramEnd.
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