Errors in PHP tell you when something goes wrong in your code. Knowing error types helps you fix problems faster.
0
0
PHP error types and levels
Introduction
When you want to find out why your PHP script stopped working.
When you want to show or hide certain errors during development or on a live website.
When you want to log errors to a file for later review.
When you want to handle different errors differently in your code.
When you want to improve your website's stability by catching mistakes early.
Syntax
PHP
<?php // Example to set error reporting level error_reporting(E_ALL); // Turn on displaying errors ini_set('display_errors', '1'); ?>
error_reporting() sets which errors PHP will report.
ini_set('display_errors', '1') turns on showing errors on the screen.
Examples
This sets PHP to report only errors, warnings, and parse errors.
PHP
<?php error_reporting(E_ERROR | E_WARNING | E_PARSE); ?>
This reports all errors except notices, which are minor warnings.
PHP
<?php error_reporting(E_ALL & ~E_NOTICE); ?>
This turns off showing errors on the screen, useful for live sites.
PHP
<?php ini_set('display_errors', '0'); ?>
Sample Program
This program turns on all error reporting and displays errors. It then causes a warning by trying to include a file that does not exist and a notice by using a variable that was never set.
PHP
<?php // Turn on all error reporting error_reporting(E_ALL); ini_set('display_errors', '1'); // Trigger a warning by including a missing file include('missingfile.php'); // Trigger a notice by using an undefined variable echo $undefinedVar; ?>
OutputSuccess
Important Notes
Errors have different levels: E_ERROR (fatal), E_WARNING (non-fatal), E_NOTICE (minor).
Use error_reporting() to control which errors you want to see.
On live sites, it's safer to hide errors from users and log them instead.
Summary
PHP errors help you find and fix problems in your code.
You can control which errors to show using error_reporting().
Displaying errors is useful during development but should be off on live sites.