PHP development basic tutorial Error

1. Introduction to the problem

In the past when we were writing code, we often saw: the function name was written wrong, and the semicolon was forgotten. Various errors will be reported if the function is redefined.

In development, displaying errors is very beneficial to our development. Because displaying errors can help us quickly locate errors and solve problems.

In the production environment (i.e., public network), websites, microsites, mobile websites, mobile interfaces, etc. are accessible to others.

If the error is displayed, it is easy to expose:

  • The server’s file path and file storage specifications

  • Some people I like to use personal names and can reversely infer passwords through social engineering

  • Sometimes the address of the mysql database server is exposed
    ... ...etc

The above information is particularly easy to be used by people with ulterior motives on the Internet.

For example, in the following code, we fully expose our server-side file storage path, framework information, etc. without adding semicolons. As follows:

<?php
$fp = fopen('abc.txt','a+')
fwrite($fp,'abc');
fclose($fp);
?>

An error was reported:

59.png

The Chinese translation of the error message is:
Parse error: syntax error. The accident occurred near line 5 of fwrite in the /home/vagrant/Code/Laravel/public/index.php file.

In this chapter, let’s try to solve this problem


2. Suppress display errors

In php .ini configuration file. We can control the error display status of php.

There is a special configuration item in php.ini:

display_errors

This option sets whether to output the error message to the web page or to the user Hide without showing.

The status of this value is on or off, and the value can also be set to 1 or 0.

If the value of display_error is set to 0 or off, the error will not be displayed on the page. If it is set to 1 or on, the error message will be displayed.

Question: What should I do if I don’t have the status permission to modify the server php.ini?

You can use ini_set.

<?php
ini_set('display_errors' , 0 );
?>

The above code is also equivalent to modifying the value of display_errors in php.ini. However, it only takes effect in the current php code.

Question: What should I do if I want to obtain the configuration item status of php.ini?

You can use ini_get (parameter item) to get the value of the parameter.

Example:

<?php
echo '服务器中display_errors的状态为' . ini_get('display_errors');
?>

Note: After modifying the php.ini file, you need to restart the server.


3. Error reporting level

1. Error type

php everyone The most common error display:


60.png


##Among the above types:

  • error is the most serious and must be resolved. Otherwise, the program cannot continue to execute.

  • warning is also very important. Tong also must be solved. If it is clear and intentional, there is no need to deal with it.

  • notice You can ignore it. But in some companies, project standards are particularly high. It must also be solved in projects with high standard requirements. Because notice will affect the execution efficiency of PHP. Usually happens when function is undefined etc.

  • Parse errors refer to grammatical errors and typographical errors, which must be resolved

  • Represents all errors of all types

Other error items that need to be understood based on the above expansion:


61.png


##In the learning process, just understand the above types. Because you basically won't encounter it. If you encounter it, you can check this book or the manual to find out.

2.error_reporting Report error type

error_reporting refers to error reporting. There is also such a parameter in php.ini. this parameter. Determines which error types the PHP engine records, reports, and displays.

1), error_reporting parameter in php.ini.

If the error_reporting parameter is set to 0. Errors in the entire PHP engine will not be displayed, output, or recorded. It will not be recorded in the logging that will be discussed in the next chapter.

If we want to display all errors, we can write:

error_reporting = E_ALL

If we want to display all errors but exclude prompts, we can write this parameter as:

error_reporting = E_ALL & ~ E_NOTICE

Show all errors, but exclude tips, compatibility and future compatibility. It can be written as:

error_reporting = E_ALL & ~E_NOTICE & ~E_STRICT & ~E_DEPRECATED

2). In some cases, we do not have permission to operate the php.ini file and want to What should I do to control error_reporting?

At the beginning of the running xxxx.php file, we can use the error_reporting() function to achieve the goal.

The demo code is as follows:

<?php
//关闭了所有的错误显示
error_reporting(0);
//显示所有错误
//error_reporting(E_ALL);
//显示所有错误,但不显示提示
//error_reporting(E_ALL & ~ E_NOTICE);
?>

You can try the above code and try to write the wrong code intentionally. Whether the specified error will be displayed in the current file.

[Expand and understand knowledge points]: The @ symbol is a single line that we have learned before that does not display errors. Please do not use the @ symbol or use it sparingly.

Let’s read a non-existent file and demonstrate the implementation process with this PHP code:

<?php
//读取一个不存在的adsaf.txt文件,用@符抑制错误
@$fp = fopen('adsaf.txt','r');
?>
@符效率较低,它在php内核中的实现过程是:
<?php
//关闭错误
error_reporting(0);
 
//读取一个不存在的文件,显示错误
 
//显示错误
error_reporting(E_ALL & ~ E_NOTICE);
?>


4. Error record log

In some companies, there are special log collection systems. The log collection system will silently help you collect errors, warnings, and prompts behind the scenes.

There are also some companies that do not have a dedicated log collection system and collect the running logs from the server through files.

Among them: PHP errors and warnings must be collected.

Then the question comes - if users are not allowed to see it, and the error reporting level is set, how to collect errors into the log system?

Here are the relevant configuration items that need to be used in php.ini. These two configuration items are:


62.png


Explanation:

  • The log_errors and log_errors_max_len in the table are very easy to understand.

  • And error_log specifies the path on which errors will be stored. The syslog in the configuration items may be a bit difficult to understand. syslog refers to system recording. Windows system is in the log collector of the computer. Linux defaults to: /etc/syslog.conf

[Extended] Learn about the knowledge points. If the Linux system is started or log collection is modified. May be stored on third-party dedicated log collection servers.

In addition, PHP also specially prepared a custom error log function for us:

bool error_log (string $error message[, int $error message type = 0 [, string $storage Target]] )

This function can send error information to the error log of the web server, or to a file.

Commonly used error message types:


63.png


##Example:

<?php
//无法连接到数据库服务器,直接记录到php.ini 中的error_log指定位置
error_log("无法连接到数据库服务器服务器");
//可以发送邮件,但是php.ini必须配置过邮件系统
error_log('可以用邮件报告错误,让运维人员半夜起床干活',1 ,'liwenkai@phpxy.com');
//记录在指定的位置
error_log("我是一个错误哟", 3, "d:/test/my-errors.log");
?>

Note: Sending emails in error_log may be unfamiliar to beginners, so you don’t need to master some knowledge.




##5. Custom error handling functionThe starting point of this piece of knowledge is a bit high. Most people have no experience in software engineering or custom error handling, and it is difficult to imagine usage scenarios. If you want to skip this block of learning, you can, and we support it.

This knowledge point does not have many practical application scenarios. If you have plans to start writing your own framework, or if you have completed the first project of this book.

Two functions commonly used for user-defined errors:

set_error_handler (callable $callback error handling function)

Set a user-defined error handling function

trigger_error (string $error_msg)

Generate a user-level error/warning/notice message

<?php
//定义一个自定义的错误处理函数
function customError($errno, $errstr, $errfile, $errline) {
    //输出错误消息
    echo "<b>Custom error:</b> [$errno] $errstr<br />";
    //输出错误文件和错误行
    echo "Error on line $errline in $errfile<br />";
    echo "Ending Script";
    //中止程序运行
    exit;
}
//使用set_error_handler 绑定用户自定义函数
set_error_handler("customError");
$test=2;
//触发自定义错误
if ($test > 1) {
    trigger_error("A custom error has been triggered");
}
?>

Continuing Learning
||
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php.cn</title> </head> <body> <?php echo "Hello World!!!"; ?> </body> </html>
submitReset Code