PHP error reporting is a very important built-in function in PHP. In this blog, we will discuss the PHP reporting function in detail with its syntax, parameters, return value, and some examples.
PHP (Hypertext Preprocessor) is a server-side scripting language for designing responsive web pages. The errors or mistakes in a PHP code are called PHP errors. These errors fail to generate the expected results and need to be resolved as soon as possible.
Types of PHP Error
PHP errors can be classified into three main categories:
Syntax errors: Whenever there is an error in the syntax of the PHP code, it is known as a syntax error. It may occur due to a missing semicolon, brackets, etc.
Runtime errors: The errors that occur during execution are known as PHP errors. It may occur due to access to an undefined variable or a wrong call to a function.
Logical errors: This type of PHP error occurs when there is an error in the logic of the PHP code, which results in unexpected outputs.
PHP error reporting
PHP error reporting is a powerful built-in function in PHP that allows developers to identify and fix runtime, syntax, and logical errors. These errors in PHP are categorized into different levels. The error_reporting() function manages the reporting of php errors at different levels.
The PHP error reporting function lets developers see error messages and warnings corresponding to the source of the error.
The function sets the error_reporting directive at runtime based on the specification in the php.ini file.
Error level: The php error reporting function accepts only one optional argument. It states the error reporting level. If not specified, the level of the current script is taken by default.
Return value of php error reporting
The PHP error reporting function returns the error reporting level.
Change logs
Change logs in the PHP error reporting function refer to the changes made in its settings or the PHP code itself to increase the efficiency of the function. The change logs related to the error_reporting() function are:
Versions
Changes
PHP 8.0
Error level can now be set to NULL value
PHP 5.4
We have E_STRICT now as a part of E_ALL
PHP 5.3
Addition of E_DEPRECATED and E-USER-DEPRECATED
PHP 5.2
The new addition of E_RECOVERABLE_ERROR
PHP 5.0
Introduction of E_STRICT
Examples
Some examples of PHP reporting functions are:
<? php
// To turn off all the error reporting
error_reporting(0);
// To report all PHP errors
error_reporting(-1);
// To report all PHP errors
error_reporting(E_ALL);
// To report all the PHP errors except E_NOTICE
error_reporting(E_ALL & ~E_NOTICE);
// To report simple runtime errors
error_reporting(E_WARNING | E_ERROR | E_PARSE);
// E_NOTICE is also good for reporting uninitialized variables
error_reporting( E_WARNING |E_ERROR | E_PARSE | E_NOTICE);
// It is similar to error_reporting(E_ALL);
ini_set('error_reporting', E_LL);
?>
You can also try this code with Online PHP Compiler
What is the purpose of the PHP error reporting function?
The error_reporting() function is a pre-defined method in PHP that allows developers to manage the reporting of php errors at different levels. The PHP error reporting function lets developers see error messages and warnings corresponding to the source of the error.
What is the parameter accepted by the PHP error reporting function?
The PHP error reporting function accepts only one optional argument of error_level. It states the error reporting level. If not specified, the level of the current script is taken by default.
What are the different errors in PHP?
There are different types of errors in PHP. These include the parse errors that correspond to the syntax errors in the code, fatal errors that prevent the script from executing, warnings that refer to serious issues, and notices which provide useful feedback.
Conclusion
Kudos, Ninja, on making it to the finish line of this article! We have understood the PHP error reporting() function in detail.
We hope this blog has helped you understand the concept of php error reporting better. Keep learning! We recommend you read some of our other articles on PHP:
But suppose you have just started your learning process and are looking for questions from tech giants like Amazon, Microsoft, Uber, etc. You must look at the problems, interview experiences, and interview bundles for placement preparations.