Skip to content

PHP Tutorial Error Handling

PHP Errors

Lesson 186 of 369 2 min read By Deepak Paswan Updated Oct 5, 2026

Errors are PHP telling you something is wrong. Learning to read them is one of the most valuable skills in programming.

The main kinds

KindWhat it meansDoes the script continue?
Parse errorThe code has a syntax mistakeNo, it never starts
Fatal errorSomething impossible happened (call to an undefined function, uncaught exception)No, it stops
WarningSomething is wrong, but PHP can go on (missing file in include, undefined variable)Yes
NoticeSomething might be wrong (rare in PHP 8)Yes
DeprecatedYou use a feature that will be removedYes

What an error message looks like

index.php
PHP
<?php
echo $missing;
Output
OUTPUT
Warning: Undefined variable $missing in /path/to/index.php on line 2

Read it in parts:

  1. Warning is the level.
  2. Undefined variable $missing is the problem.
  3. /path/to/index.php and on line 2 is where.

A fatal error

index.php
PHP
<?php
echo "Before\n";
undefinedFunction();
echo "After";
Output
OUTPUT
Before

Fatal error: Uncaught Error: Call to undefined function undefinedFunction() in /path/to/index.php:3
Stack trace:
#0 {main}
  thrown in /path/to/index.php on line 3

PHP printed Before, then stopped. After never ran. In PHP 8, most fatal errors are real Error objects that you can catch (see Exceptions).

Deprecated

index.php
PHP
<?php
function oldStyle($a = 1, $b)
{
    return $b;
}

echo oldStyle(1, 2);
Output
OUTPUT
Deprecated: Optional parameter $a declared before required parameter $b is implicitly treated as a required parameter in /path/to/index.php on line 2
2

The code still works, but a future PHP version may stop allowing it. Fix deprecations when you see them.

Error levels

PHP has constants for each level: E_ERROR, E_WARNING, E_NOTICE, E_DEPRECATED, E_ALL and more. error_reporting() decides which are reported.

index.php
PHP
<?php
error_reporting(E_ALL);   // report everything: use this while developing

echo error_reporting() === E_ALL ? "All errors reported" : "Some hidden";
Output
OUTPUT
All errors reported

Since PHP 8, the default error_reporting is E_ALL.

Where do errors go?

Two separate settings:

  • display_errors: show errors on the page. Good for development, never for a live site.
  • log_errors: write errors to a log file. Always on in production.

See Production Error Handling.

What changed in PHP 8

Several mistakes that used to be quiet notices are now louder:

  • “Undefined variable” and “Undefined array key” are warnings.
  • Passing the wrong type to a built-in function throws a TypeError.
  • Dividing by zero throws a DivisionByZeroError.
lightbulb
Treat warnings as bugs
A script that “works but shows warnings” hides real problems. Fix every warning, even ones that look harmless.