How to get error messages in PHP

PHPz
Release: 2023-04-04 10:40:01
Original
450 people have browsed it

PHP is a widely used server-side scripting language that is easy to learn and highly flexible. However, like all programming languages, PHP also has its shortcomings and errors, such as syntax errors and logic errors. During the development and debugging process, it is crucial to obtain and resolve these error messages in a timely manner. This article will introduce how to get error information in PHP.

1. Turn on PHP error messages

In PHP, error messages will not be output by default. You need to configure the php.ini file to enable the output of error messages. You can open the php.ini file and find the following two key lines:

display_errors = Off
error_reporting = E_ALL & ~E_DEPRECATED & ~E_STRICT
Copy after login

Change display_errors = Off to display_errors = On, and then change error_reporting Change the line to:

error_reporting = E_ALL
Copy after login

This will enable all error reporting, including all error levels. After turning on error information, every error in the PHP script will be output on the web page, including warnings, prompts, and fatal errors.

2. Get the latest PHP error message

In a PHP script, you can use PHP’s own error_get_last() function to get the latest error message. This function returns an array containing the error information type, message, file path, etc.

Here is an example:

<?php
echo $test;
print_r(error_get_last());
?>
Copy after login

In the above code, the $test variable is not defined, therefore, this will result in a fatal error. This error message will be stored as the last error and then retrieved through the error_get_last() function. The output result is as follows:

Array
(
    [type] => 8
    [message] => Undefined variable: test
    [file] => /var/www/html/test.php
    [line] => 2
)
Copy after login

3. Register error handler

In addition to using the error_get_last() function, you can also obtain error information by registering an error handler. PHP provides a set_error_handler() function for customizing PHP's behavior when an error occurs. This function needs to receive as its parameter the name of a function that can be used to handle caught errors.

The following is an example:

<?php
function error_handler($errno, $errstr, $errfile, $errline) {
    echo "Error: [$errno] $errstr - $errfile:$errline";
}

set_error_handler("error_handler");

echo $test;
?>
Copy after login

In the above code, we define a function named error_handler() to handle errors. Then pass the function name to the set_error_handler() function to register. This will result in a fatal error when variable $test is not defined. The error handler will catch the error and output the following result:

Error: [8] Undefined variable: test - /var/www/html/test.php:7
Copy after login

IV. Conclusion

In the process of developing PHP scripts, it is crucial to obtain and handle error information in a timely manner. Various methods, from turning on PHP error messages to getting the latest PHP error messages, and using registered error handlers, can help us better handle and debug errors in PHP development.

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

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