Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
PHP Error
3.
Types of PHP Error
4.
PHP error reporting 
5.
Syntax of PHP error reporting
6.
Parameters of php error reporting
7.
Return value of php error reporting
8.
Change logs
9.
Examples
10.
Frequently Asked Questions
10.1.
What is the purpose of the PHP error reporting function?
10.2.
What is the parameter accepted by the PHP error reporting function?
10.3.
What are the different errors in PHP?
11.
Conclusion 
Last Updated: Mar 27, 2024
Easy

PHP Error Reporting

Introduction

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 error reporting

Also, read about the PHP introduction here.

PHP Error

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

types of php errors

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.

Also, check out our article on PHP File Handling.

Syntax of PHP error reporting

The syntax of the error reporting function in PHP is as follows:

error_reporting(int $level);
You can also try this code with Online PHP Compiler
Run Code

Parameters of php error reporting

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
Run Code

Must Read PHP Projects With Source Code

Frequently Asked Questions

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: 

  1. functions in PHP
  2. OOPs in PHP
  3. PHP Arrays
     

Refer to our Guided Path to upskill yourself in DSACompetitive ProgrammingJavaScriptSystem Design, and many more! If you want to test your competency in coding, you may check out the mock test series and participate in the contests hosted on Coding Ninjas Studio!

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 problemsinterview experiences, and interview bundles for placement preparations.

Best of Luck! 

Happy Learning!

Live masterclass