Errors and exceptions in PHP

Source: Internet
Author: User
: This article mainly introduces errors and exceptions in PHP. For more information about PHP tutorials, see.

Errors and exceptions are two completely different concepts.

Error

Error Type

16 in total

Value Constant Description Remarks
1 E_ERROR (integer) Fatal runtime error. Such errors are usually irrecoverable, such as problems caused by memory allocation. The consequence is that the script stops running.
2 E_WARNING (integer) Run-time warning (non-fatal error ). Only prompts are provided, but the script does not stop running.
4 E_PARSE (integer) Syntax parsing error during compilation. Parsing errors are only generated by the analyzer.
8 E_NOTICE (integer) Runtime notification. Indicates that the script may be incorrectly displayed, but similar notifications may occur in scripts that can run normally.
16 E_CORE_ERROR (integer) A fatal error occurs during PHP initialization and startup. This error is similar to E_ERROR, but is generated by the core of the PHP engine. Since PHP 4
32 E_CORE_WARNING (integer) Warning during PHP initialization startup (non-fatal error ). It is similar to E_WARNING, but it is generated by the core of the PHP engine. Since PHP 4
64 E_COMPILE_ERROR (integer) Fatal compilation error. It is similar to E_ERROR, but it is generated by the Zend script engine. Since PHP 4
128 E_COMPILE_WARNING (integer) Compile-time warning (non-fatal error ). It is similar to E_WARNING, but it is generated by the Zend script engine. Since PHP 4
256 E_USER_ERROR (integer) Error message generated by the user. It is similar to E_ERROR, but it is generated by the user using the PHP function trigger_error () in the code. Since PHP 4
512 E_USER_WARNING (integer) User-generated warning information. It is similar to E_WARNING, but it is generated by the user using the PHP function trigger_error () in the code. Since PHP 4
1024 E_USER_NOTICE (integer) User-generated notification information. It is similar to E_NOTICE, but it is generated by the user using the PHP function trigger_error () in the code. Since PHP 4
2048 E_STRICT (integer) Enable PHP code modification suggestions to ensure the best code interoperability and forward compatibility. Since PHP 5
4096 E_RECOVERABLE_ERROR (integer) Possible fatal errors. It indicates that a potentially dangerous error has occurred, but it has not caused the PHP engine to be unstable. If the error is not captured by the custom handle (see set_error_handler (), it becomes an E_ERROR and the script stops running. Since PHP 5.2.0
8192 E_DEPRECATED (integer) Runtime notification. When enabled, a warning will be given to code that may not work properly in future versions. Since PHP 5.3.0
16384 E_USER_DEPRECATED (integer) User-generated warning information. It is similar to E_DEPRECATED, but it is generated by the user using the PHP function trigger_error () in the code. Since PHP 5.3.0
30719 E_ALL (integer) All error and warning information of the E_STRICT field. 30719 in PHP 5.3.x, 6143 in PHP 5.2.x, 2047 previusly

Error Level

(1) Deprecated is not recommended

if (ereg('/llo/', 'hello world')) {    echo 'yes2';} else {    echo 'no2';}

Output

Deprecated: Function ereg() is deprecated in /Users/weiheli/www/php/003.php on line 3no

(2) Notice notification level

$arr = ['a'=>'aaa', 'b'=>'bbb'];echo $arr[a];

Output

Notice: Use of undefined constant a - assumed 'a' in /Users/weiheli/www/php/003.php on line 4aaa

(3) Warning level

settype($var, 'abc');echo $var;

Output

Warning: settype(): Invalid type in /Users/weiheli/www/php/003.php on line 3

(4) Fatal error Fatal level

// Call the undefined function fn (12 );

Output

Fatal error: Call to undefined function fn() in /Users/weiheli/www/php/003.php on line 3

(5) Parse error parsing error

echo 'hello world'

Output

Parse error: parse error, expecting `','' or `';'' in /Users/weiheli/www/php/003.php on line 3

(6) E_USER _ user-level error

Configuration error

; Whether an error is displayed. Display_errors = On is always displayed for parsing errors. which errors are displayed? error_reporting = E_ALL &~ E_DEPRECATED &~ E_STRICTerror_reporting = E_ALL

You can set the error_reporting command at runtime.

error_reporting()

You can also useini_setFunction settings

User throw error

trigger_error(string $error_msg [, int $error_type = E_USER_NOTICE ])

Record logs

Configuration in php. ini

Whether to record the error log log_errors = On the maximum number of bytes of the error log log_errors_max_len = 1024 whether to ignore the duplicate error ignore_repeated_errors whether to ignore the duplicate information source ignore_repeated_source and save it

Logging functions

error_log()openlog() syslog()closelog() 

Custom error handling

Set_error_handler-set a user-defined error handling function

Exception

try {    $num = 1 / 0;} catch (Exception $e) {    echo $e->getMessage();}Warning: Division by zero in /Users/weiheli/www/php/003.php on line 4

As shown in the code above, PHP does not automatically throw an exception and must be used.throwThis is different from Java.

Try {$ num = 0; if ($ num = 0) {throw new Exception ('cannot be 0');} catch (Exception $ e) {echo $ e-> getMessage (); echo 'abc ';}

Built-in exception classes such as PDO are not neededthrow

throwSubsequent statements will not be executed

Try cannot be used independently

Fatal error: Cannot use try without catch or finally in /Users/weiheli/www/php/003.php on line 8

Custom exception handling

Set_exception_handler-set a user-defined exception handling function

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

The above describes errors and exceptions in PHP, including related content, and hopes to help those who are interested in PHP tutorials.

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.