"PHP Technology" PHP error types and Shielding methods

Source: Internet
Author: User
Tags deprecated parse error php error

"PHP Technology" PHP error types and Shielding methods simple source of life: Site Published: 2016-10-30 08:58:12 view: 7 times First, PHP error type

First of all, what we need to know is how we can show our errors:

By adding two lines of code to the code:

1. Display all error and warning messages except E_strict

Error_reporting (E_all);

2. Whether to display errors on the output

Ini_set (' display_errors ', ' on ');

With the above two operations, we can display our error message on the page.

Second, we need to know what types of errors PHP has:

According to official Chinese documents, there are 16 error levels:

Address: http://php.net/manual/zh/errorfunc.constants.php

In the document already very clearly describes the level of error and the cause of the error, here we choose a few more representative errors, to give you detailed description:

1. E_error

Fatal run-time error. Such errors are generally unrecoverable situations, such as problems caused by memory allocations. The result is that the script terminates and no longer continues to run. The fatal error is usually displayed on the page, which can be caused by access to the function code that does not exist, the server execution time timeout, the call to a nonexistent class code, and so on.

Such as: class does not exist

$goodsModel = new Goods ();

Fatal error:class ' Goods ' not found in D:\web\error.php on line 3

2. E_parse

Compile-time syntax parsing error. Parsing errors are generated only by the parser. This syntax error can be avoided by using a professional IDE, such as Phpstorm,zend studio and so on.

Example: no variable value set

$a =;

Parse error:syntax error, unexpected '; ' in D:\web\error.php on line 3

3. e_warning

Run-time warning (non-fatal error). Only the prompt is given, but the script does not terminate the run.

Such as:

Include ' xxx.php ';

Echo ' keep running ';

Warning:include (xxx.php): Failed to open stream:no such file or directory in D:\web\error.php on line 3

Warning:include (): Failed opening ' xxx.php ' for inclusion (include_path= '.; C:\php\pear ') in D:\web\error.php on line 3

Continue running

4. E_notice

Run-time notifications. Indicates that the script encountered a situation that might behave as an error, but there may be similar notifications in a script that can run correctly. The level of this error is slightly lower.

Such as:

Echo $c;

notice:undefined variable:c in D:\web\error.php on line 8

5. e_deprecated

Run-time notifications. When enabled, warns you about code that might not work correctly in a future release.

Such as:

When we use PHP5.5 (or higher), if we use the mysql_connect (() function to connect to the database will be an error, we recommend the use of updated MySQL connection mode

$conn = mysql_connect (' localhost ', ' root ', ' admin ');

Deprecated:mysql_connect (): The MySQL extension is Deprecated and would be a removed in the Future:use mysqli or PDO Instea D in D:\web\error.php on line 4

There are many error types that you can learn more about in the documentation.

Second, shielding method

1. Masking a function error message

With the @ sign, you can mask a function error message, which is not an error.

Such as:

$conn = @mysql_connect (' localhost ', ' root ', admin);

2, shielding a type of error

2.1 Masking in code

Syntax: error_reporting (e_all ^ error constant) or error_reporting (E_all & ~ Error constant)

Such as:

Error_reporting (e_all ^ e_deprecated);

$conn = mysql_connect (' localhost ', ' root ', ' admin ');

2.2 Masking in configuration file php.ini

Syntax: error_reporting = e_all & ~ Error Constants

Such as:

error_reporting = E_all & ~ e_deprecated & ~ E_notice

3. Block all Errors

Way:

In the Code error_reporting (0) or modify the configuration file error_reporting = 0

However, we generally recommend not to turn off all errors at work, but rather log all error messages and not display error messages on the page. Then I'll show you how to catch errors and log logs.

PHP Technology PHP Error types and masking methods

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.