E_NOTICE vs E_WARNING vs E_ERROR in PHP: Key Differences and Usage
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 Level | Severity | Script Execution | Common Causes | Visibility |
|---|---|---|---|---|
| E_NOTICE | Low | Continues | Undefined variables, minor mistakes | Shown if error reporting includes notices |
| E_WARNING | Medium | Continues | Include failures, wrong function arguments | Shown by default |
| E_ERROR | High | Stops immediately | Fatal errors like calling undefined functions | Always 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 // E_NOTICE example: Using an undefined variable echo $undefinedVar; // Script continues after notice echo "Script still runs.\n"; ?>
E_WARNING Equivalent
This example triggers an E_WARNING by trying to include a missing file.
<?php // E_WARNING example: Including a missing file include 'missingfile.php'; // Script continues after warning echo "Script still runs despite warning.\n"; ?>
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.