PHP supports an error control operator: @. Any error messages that may be generated by the expression are ignored until it is placed in a PHP expression.
If a custom error handler is set with Set_error_handler (), it will still be called, but this error handler can (and should) call Error_reporting (), which returns 0 if the function has @ at the top of the error statement.
If the track_errors attribute is activated, any error information generated by the expression is stored in the variable $php _errormsg. This variable is overwritten every time an error occurs, so check it out as soon as possible if you want to use it.
PHP ignores error symbol @
@ is an error-ignoring character in the PHP language, and any error messages that may be generated by the expression are ignored until it is placed in a PHP expression. For example:
@include ("file.php");
It is important to note that the @ operator is only valid for an expression. It can be understood that if you can get a value from somewhere, you can precede it with the @ operator. For example, you can put it before variables, functions, and include calls, constants, and so on. It cannot be placed before the definition of a function or class, nor can it be used for conditional structures such as if and foreach.
In addition, @ is usually used with or die (), for example:
(@include ("file.php")) Or Die ("Could not find file.php!");
The above statement means that if the inclusion of file.php is unsuccessful, such as if the file.php file does not exist, the page will print "Could not find file.php!" and immediately stop the execution of the program.
If this character is not added, then when the include ("file.php") returns an error, the page prints:
Warning:include (file.php) [function.include]: failed to open stream:no such file or Directorywarning:include () [Functio N.include]: Failed opening ' file.php ' for inclusion (include_path= '.; C:\php5\pear ')
Some usage about or die ()
Die () function is very familiar to everyone, but or die () such writing, the results of some of the effects you may not know.
First understand the die ().
Die Definition and usage
The die () function outputs a message and exits the current script.
Grammar
Die (status)
Status Required. Specifies the message or status number to write before exiting the script. The status number is not written to the output.
If the status is a string, the function outputs the string before exiting.
If the status is an integer, this value is used as the exit state. The value of the exit status is between 0 and 254. Exit status 255 is reserved by PHP and will not be used. Status 0 is used to successfully terminate the program.
Note: If PHP has a version number greater than or equal to 4.2.0, the parameter will not be output if the status is an integer.
php or Die () statement
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, commonly used in debugging programs.