Home > Backend Development > PHP Problem > How to catch error prompts in php

How to catch error prompts in php

藏色散人
Release: 2023-03-13 20:36:01
Original
3971 people have browsed it

How to capture error prompts in php: 1. Output all errors through "error_reporting(E_ALL);"; 2. Use "try...catch" to catch exceptions; 3. Use the set_error_handler() function to host errors handler.

How to catch error prompts in php

The operating environment of this article: windows7 system, PHP7.1 version, DELL G3 computer

How to capture error prompts in php ?

php exception handling capture error sorting

There are three types of PHP error reports:

1. Error, syntax parsing error, fatal error

2. Warning

3. Pay attention to

Consequences:

Error-> Fatal error will terminate the execution of the downloaded program. If there is a syntax error, PHP will not work at all. implement.

Warning -> It will not terminate the run, but it will affect the results.

Note -> will not terminate execution and will not affect the results.

In order to give users a better experience, we mask all error output, which is output, not display. But in this case, the administrator will not be able to see the error. The error

is not displayed on the page, but a log is generated for the administrator to view.

error_reporting(~E_ALL) blocks all output. Naturally, the administrator cannot see it. I don't want to block all output, I just want to block all display

ini_set('display_errors','off'); means that the display of errors on all pages is blocked, but the output of errors is not blocked.

ini_set('log_errors','on'); 
//开启日志写入功能 
ini_set('error_log','myerror.log');
 
//日志的存放位置 
ini_set('display_errors','off'); 
//屏蔽页面显示 
error_reporting(E_ALL); 
//输出所有错误
 
echo 123; echo $str; 
//这个会出现一个注意,表示未声明变量 
echo date(); 
//警告,没有传参 
echo dae(); 
 
//致命错误,找不到这个函数 
echo 123;
Copy after login

Exception:

In the PHP language, all exceptions must be thrown by themselves, unlike languages ​​like JAVA that will automatically throw exceptions. This is exactly what is in the PHP source code One of the

reasons

that you rarely see exception handling statements.

Exceptions and errors:

Exceptions refer to conditions that do not meet expectations and are different from the normal process during program operation. The error is its own problem, which is caused by illegal syntax or environmental problems that prevent the compiler from passing the check settings and running.

Because PHP did not have exception handling at the beginning, it was later imitated in order to enter enterprise-level development. Once PHP encounters abnormal code, in most cases, it will directly throw an error instead of an exception.

php can only use try...catch to catch exceptions after you throw an exception (this is generally the case, and some exceptions can be automatically caught).

Exceptions are usually used in PHP in the following scenarios:

1. Pessimistic prediction of the program: If you think that your code cannot handle various foreseeable situations and unforeseeable situations one by one Case.

2. Program needs and business concerns: If the data consistency requirements are very high, you can use try...catch to minimize the logical interruption damage caused by exceptions and remedy them

After processing, the integrity of the business logic will not be affected.

3. Language-level robustness requirements: By accurately controlling the runtime process, when the program is interrupted, use try...catch to predictably narrow the scope of possible errors, catch exceptions in a timely manner, and

Provide corresponding remedies.

Errors in PHP:

Errors are situations that cause the script to run abnormally.

The main error levels in php are as follows:

deprecated: The lowest level error, which means "not recommended, not recommended". For example, if you use the ereg series of regular functions in PHP 5, it will appear. This type of error is usually caused by using deprecated or outdated functions or syntax. It does not affect the normal operation of the program, but it is recommended to correct it.

notice: Generally refers to inappropriate places in the grammar. This error will be reported if a variable is used but is not defined. Does not affect the normal flow of the program.

warning: A higher-level error. This error occurs when there is a very inappropriate situation in the syntax, such as function parameter mismatch. As a result, the expected results cannot be obtained and the code needs to be modified.

fetal error: Fatal error, directly causing the program to terminate. Such errors must be corrected.

prase error: Syntax parsing error. The above are all runtime errors. This error will be thrown before running.

Custom error handler:

You can use the set_error_handler() function to host the error handler and customize the error handling process.

If you want to cancel hosting, you can use restore_error_handler() in the same page to cancel hosting; if you want to throw an error yourself, you can use the

trigger_error() function.

Recommended learning: "

PHP Video Tutorial

"

The above is the detailed content of How to catch error prompts in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template