Home >Backend Development >PHP Tutorial >Classification and processing methods of PHP error messages
Classification and processing methods of PHP error messages
2.1 Syntax error
Syntax error is one of the most common errors in the code and the easiest to find. When there is a syntax error in our code, the PHP parser will display the corresponding error message and indicate the specific line number of the code.
Example 1: Syntax error
<?php echo "Hello, World!' ?>
Error message:
Parse error: syntax error, unexpected '$' in example.php on line 2
In the above In the example, the syntax error occurs because the string quotes are not closed.
Processing method:
Under normal circumstances, we only need to find the corresponding line of code and check for syntax errors according to the error message.
2.2 Run-time errors
Run-time errors refer to errors that occur during the running of the code and are also one of the most common errors. Runtime errors may cause code interruptions or exceptions.
Example 2: Runtime Error
<?php $number = 10; echo $number / 0; ?>
Error message:
Warning: Division by zero in example.php on line 3
In the above example, run The error occurs because the divisor is 0.
Handling method:
For runtime errors, we can use conditional statements or exception handling mechanisms to avoid and handle errors.
2.3 Logic Error
Logic error refers to an error in the logic of the code. It usually does not result in an error message, but will affect the normal execution of the program.
Example 3: Logic Error
<?php $number = 10; if ($number >= 0) { echo "Positive number"; } else { echo "Negative number"; } ?>
In the above example, the logic error is caused by incorrect condition judgment.
Handling method:
For logic errors, we need to carefully check the logic of the code to ensure the correctness of the conditional judgment and algorithm.
3.1 Turn on error reporting
During the PHP development process, we can turn on error reporting by setting the php.ini file. Just add the following code to the php.ini file:
display_errors = on error_reporting = E_ALL
3.2 Using error handling functions
PHP provides some built-in error handling functions that can be used to capture and handle different types of errors.
3.2.1 die() function
The die() function is used to output error information and terminate the execution of the script.
Example 4: Use the die() function to process error information
<?php $number = 10; if ($number > 5) { die("Number is greater than 5"); } else { echo "Number is less than or equal to 5"; } ?>
3.2.2 set_error_handler() function
The set_error_handler() function is used to customize the error handling function, and the error information can be Output to a log file or other location.
Example 5: Use the set_error_handler() function to process error information
<?php function customErrorHandler($errno, $errstr, $errfile, $errline) { // 将报错信息写入日志文件 $logMessage = "Error: [$errno] $errstr - $errfile:$errline"; error_log($logMessage, 3, "/var/log/php-error.log"); } // 使用自定义错误处理函数 set_error_handler("customErrorHandler"); // 产生一个报错信息 echo $undefinedVariable; ?>
The custom error handling function in the above example writes the error information to the log file.
The above is the detailed content of Classification and processing methods of PHP error messages. For more information, please follow other related articles on the PHP Chinese website!