Blog |

What is E_WARNING in PHP?

What is E_WARNING in PHP?
Table of Contents

The PHP E_WARNING constant refers to run-time warnings. Warnings are non-fatal errors in PHP that do not halt script execution.

What Causes E_WARNING

PHP Warnings are errors that are issued when something unexpected or unwanted occurs in code. They are usually triggered because of minor mistakes such as:

  • Referring to a file that does not exist.
  • Wrong or missing arguments for inbuilt function calls.
  • Sending HTTP headers after sending HTTP body output.

It is considered good practice to fix warnings as they can potentially lead to serious issues over time.

How to Enable E_WARNING

It is best practice to enable E_WARNING during development since it warns developers about possible bugs in code. This can help with issues such as missing inbuilt function arguments and saves time when debugging.

E_WARNING can be enabled using the error_reporting() function:

error_reporting(E_WARNING);

This will show only E_WARNING errors. However, it is recommended to show all errors during development. This can be done by enabling E_ALL:

error_reporting(E_ALL);

E_WARNING Example

Here's an example of a PHP E_WARNING issued when calling an inbuilt function with a missing parameter:

<?php
    $a = 1;
    var_dump();
?>

When the above script is executed, a Warning is generated since the var_dump() function expects an argument:

PHP Warning: var_dump() expects at least 1 parameter, 0 given in main.php on line 3

How to Fix E_WARNING

PHP Warnings can be identified by inspecting the warning message and the line numbers in code where the issue exists. The issue can then be fixed in code to resolve the Warning.

In the earlier example, the warning message can be inspected:

PHP Warning:  var_dump() expects at least 1 parameter, 0 given in main.php on line 3

Here, it can be seen that the issue occurs due to a missing parameter for the var_dump() function on line 3. This issue can be fixed by passing a parameter in the function call:

<?php
    $a = 1;
    var_dump($a);
?>

The above code executes successfully and produces the correct output as expected:

int(1)

Track, Analyze and Manage Errors With Rollbar

Managing errors and exceptions in your code is challenging. It can make deploying production code an unnerving experience. Being able to track, analyze, and manage errors in real-time can help you to proceed with more confidence. Rollbar automates error monitoring and triaging, making fixing PHP errors easier than ever. Try it today!

Related Resources

"Rollbar allows us to go from alerting to impact analysis and resolution in a matter of minutes. Without it we would be flying blind."

Error Monitoring

Start continuously improving your code today.

Get Started Shape