John Mwaniki /   18 Apr 2023

Differences between Notice, Warning and Errors in PHP

When programming in PHP, you are most likely to encounter various errors from time to time whereby the error messages are displayed as an output of the script when running or in the error_log file, depending on whether the error reporting is turned on or off.

These error messages are always labeled as "Notice", "Warning", or "Error". It is important to understand the differences among these types of messages in order to know how to diagnose and fix issues in your code.

Let's look at each below:

Notice

A Notice is an informational message that indicates something in the code may not be working as expected, but the script can still continue to run.

It's like an advisory message telling you that you shouldn't be doing what you're doing though it won't halt the script from running.

Example

<?php
$str = "Hi there, good morning";
echo end(explode(" ",$str));
?>

Output:

Notice: Only variables should be passed by reference in /path/to/file.php on line xx
morning

In this example, we are trying to split a string into an array using the explode() function (and space as the separator), get the value of the last element of the resulting array using the end() function, and print it on the screen. The expected output is "morning".

However, the end() function expects only a variable to be passed as an argument but we have instead passed a function to it. Thus, PHP issues a Notice stating that only variables should be passed by reference, but the script continues to run.

Warning

A Warning is a message that indicates something in the code is potentially problematic, but the script can still run. However, ignoring warnings can lead to unexpected behavior.

Example

<?php
$str = "Hello Word!";
echo $strr;
?>

Output:

Warning: Undefined variable $strr in /path/to/file.php on line xx

In this example, we tried to print a variable that has not been defined. PHP issues a Warning stating that the variable has not been defined, but the script continues to run.

Error

An Error is a message that indicates a serious problem in the code that prevents the script from running properly. Unlike notices and warnings, when an error occurs execution of the script is halted.

Example

<?php
echo "This code has an error"
?>

Output:

Parse error: syntax error, unexpected end of file, expecting "," or ";" in /path/to/file.php on line xx

In the above example, we are trying to print a string but have omitted the closing semicolon at the end of the line. PHP issues an Error stating that the script has a syntax error, specifically that a semicolon is expected but not found. Execution of the script is terminated.

Error messages are classified as Parse Errors (syntax errors) or Fatal Errors.

The Parse Errors occur when there is a syntax mistake in the PHP script. Common scenarios that cause these errors include:

  • Missing semicolon
  • Unclosed quotes
  • Unclosed braces
  • Missing or Extra parentheses

On the other hand, fatal errors occur when there are no syntax errors but what you want to do can't be done. A common example is when you try to call an undefined function.

Conclusion

In PHP, error messages are classified as "Notice", "Warning", or "Error". In this article, we have covered what each error type means and their differences.

While Notices and Warnings may not cause the script to stop running, ignoring them can lead to unexpected behavior. Errors, on the other hand, will stop the script from running altogether.

You can use the error_reporting function to control which types of messages you want to see in your development environment. However, it is recommended to turn off all types of messages in the production environment.