php or Die () statement, exit ()

Source: Internet
Author: User
Often see this statement: $file = fopen ($filename, ' r ') or Die ("Sorry, unable to open: $filename");

Or is understood here, because the data type is not differentiated in PHP, so the $file can be either int or bool, so the statement does not get an error. But the process may be some friends are not very clear. In most languages, in a statement such as BOOL or bool, a value is no longer judged if the previous value is true. This is also the case, so if the fopen function executes correctly, it returns an int value greater than 0 (which is actually "true"), and the subsequent statement will not be executed. If the fopen function fails, it will return false, then it will determine if the subsequent expression is true. The result is that after the die () is executed, no matter what is returned, the program has stopped executing, and the specified error message is displayed, and the purpose of debugging is achieved. That's it. :)

The two functions commonly used with error management are Die () and exit () (strictly speaking, they are language constructs, not functions, but who cares about them). When Die () and exit () are called in the script, the entire script is terminated. They can all be used to prevent the script from continuing, leaving some important operations, such as establishing a database connection, from occurring. You can also pass a string that will be printed in the browser to Die () and exit ().

You can usually see the use of Die () and exit () in an or conditional statement. For example

Include (' config.inc.php ') OR die (' Could not open the file. ');

After containing such a line of code, if PHP cannot contain the configuration file, the die () statement will be executed and the could not open the file message will be printed.

!defined (' Curscript ') && define (' Curscript ', ");//means that if you do not define a Curscript constant, the definition curscript is empty.

In the book and in the PHP manual, you will see a variety of variants, as it is a shortcut to handling errors (but possibly excessive) without using a custom error handler.

In fact, die and exit are equivalent, used to terminate the current script!

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