Blog |

What is E_NOTICE in PHP?

What is E_NOTICE in PHP?
Table of Contents

The PHP E_NOTICE constant refers to run-time notices. Notices indicate that the script encountered something that could indicate an error, but could also happen in the normal course of running a script.

What Causes E_NOTICE

Notices indicate minor errors in PHP that do not halt script execution. They are usually triggered because of minor mistakes in code such as:

  • Using a variable before declaring it.
  • Typos in variable names.
  • Using an ambiguous array index.

Notice messages are not serious and can often be ignored. However, it is generally considered good practice to fix them.

How to Enable E_NOTICE

It is best practice to enable E_NOTICE during development since it warns developers about possible bugs in code. This can help with issues such as unassigned values, typos etc. and saves time for debugging.

E_NOTICE can be enabled using the error_reporting() function:

error_reporting(E_NOTICE);

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

error_reporting(E_ALL);

E_NOTICE Example

Here's an example of a PHP E_NOTICE issued when using an undeclared variable:

<?php
echo $mystring;
?>

When the above script is executed, a Notice is generated since $mystring is not declared:

PHP Notice:  Undefined variable: mystring in main.php on line 2

How to Fix E_NOTICE

PHP Notices can be identified by inspecting the notice message and the line numbers in code where the issue exists. The issue can then be fixed in code to resolve the Notice.

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

PHP Notice:  Undefined variable: mystring in main.php on line 2

Here, it can be seen that the issue occurs due to an undeclared variable mystring on line 2. This issue can be fixed by declaring the variable before using it:

<?php
$mystring = 'Hello World';
echo $mystring;
?>

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

Hello World

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