0
0
PhpComparisonBeginner · 3 min read

E_NOTICE vs E_WARNING vs E_ERROR in PHP: Key Differences and Usage

In PHP, E_NOTICE signals minor issues that do not stop script execution, E_WARNING indicates more serious problems but still allows the script to continue, and E_ERROR represents fatal errors that halt the script immediately. These error levels help developers identify and handle problems with different severity.
⚖️

Quick Comparison

Here is a quick table comparing E_NOTICE, E_WARNING, and E_ERROR in PHP based on severity, script impact, and typical causes.

Error LevelSeverityScript ExecutionCommon CausesVisibility
E_NOTICELowContinuesUndefined variables, minor mistakesShown if error reporting includes notices
E_WARNINGMediumContinuesInclude failures, wrong function argumentsShown by default
E_ERRORHighStops immediatelyFatal errors like calling undefined functionsAlways shown
⚖️

Key Differences

E_NOTICE is the least severe error type in PHP. It alerts you about things that might be mistakes but do not stop the script. For example, using a variable that was not set will trigger a notice. These are often safe to ignore but useful for debugging.

E_WARNING is more serious. It signals a problem that could cause issues but does not stop the script. For example, including a missing file triggers a warning. The script continues but might behave unexpectedly.

E_ERROR is the most severe error. It means something went wrong that prevents the script from continuing, like calling a function that does not exist. When an E_ERROR occurs, PHP stops running the script immediately.

⚖️

Code Comparison

This example shows how PHP handles an E_NOTICE by using an undefined variable.

php
<?php
// E_NOTICE example: Using an undefined variable
echo $undefinedVar;

// Script continues after notice
echo "Script still runs.\n";
?>
Output
Notice: Undefined variable: undefinedVar in /path/to/script.php on line 3 Script still runs.
↔️

E_WARNING Equivalent

This example triggers an E_WARNING by trying to include a missing file.

php
<?php
// E_WARNING example: Including a missing file
include 'missingfile.php';

// Script continues after warning
echo "Script still runs despite warning.\n";
?>
Output
Warning: include(missingfile.php): failed to open stream: No such file or directory in /path/to/script.php on line 3 Warning: include(): Failed opening 'missingfile.php' for inclusion (include_path='.:/usr/share/php') in /path/to/script.php on line 3 Script still runs despite warning.
🎯

When to Use Which

Choose E_NOTICE to catch small, non-critical issues that help improve code quality without stopping execution. Use E_WARNING to detect problems that might affect functionality but allow the script to continue running, useful for recoverable errors. Use E_ERROR for critical problems that must stop the script immediately to avoid further damage or incorrect results.

Key Takeaways

E_NOTICE alerts about minor issues without stopping the script.
E_WARNING signals serious problems but allows script continuation.
E_ERROR causes immediate script termination due to fatal errors.
Use notices for debugging, warnings for recoverable errors, and errors for critical failures.
Understanding these helps write more robust and maintainable PHP code.