PHP Errors: Types, Reporting, and Error Handling

PHP errors are messages generated by the PHP engine when something goes wrong while executing a script. These errors help developers find bugs, incorrect configurations, and problems in application logic.

During development, PHP errors are useful because they point you directly to the problem. In production, displaying these errors publicly can expose sensitive information, so they should be logged instead.

PHP errors have a funny habit of appearing at the worst possible time. A missing semicolon, a wrong file path, or a tiny typo can turn a working page into a completely different story.

It may leave you staring at a blank page.

Fortunately, PHP errors are not mysterious messages from another dimension. They are clues. Once you understand the error types and know how to read them, debugging becomes much faster.

Quick Answer: How to Enable PHP Errors

To display PHP errors during development, enable error reporting using the following code at the beginning of your PHP script:

<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');

echo $undefinedVariable;
?>

The above configuration enables reporting for all PHP errors and displays them in the browser.

For a production website, avoid displaying errors to visitors. Instead, log them:

<?php
error_reporting(E_ALL);
ini_set('display_errors', '0');
ini_set('log_errors', '1');

echo $undefinedVariable;
?>

PHP will write the error details to the configured error log file, allowing developers to investigate issues without exposing internal information to users.

The error_reporting() function controls which PHP errors are reported.

Understanding PHP Error Types

PHP errors are classified into different types based on their severity and impact on script execution. Understanding these types helps you quickly identify whether a problem requires immediate attention or is only a warning.

1. Parse Errors

A parse error occurs when PHP cannot understand the syntax of your code. The script will not execute until the syntax issue is fixed.

Common causes include:

  • Missing semicolons
  • Unclosed brackets or quotes
  • Incorrect PHP syntax

Example:

<?php

echo "Hello World"

?>

The missing semicolon after the echo statement causes a parse error.

The error message usually includes the file name and line number where PHP detected the problem. However, the actual mistake can sometimes be on the previous line.

2. Fatal Errors

A fatal error is a serious error that stops script execution immediately.

For example, calling a function that does not exist results in a fatal error:

<?php

displayMessage();

?>

If the displayMessage() function is not defined, PHP stops execution and reports the error.

Fatal errors commonly happen because of:

  • Calling undefined functions or classes
  • Including missing required files
  • Exceeding memory limits
  • Type errors in strict PHP applications

3. Warnings

Warnings indicate a problem that PHP can recover from. The script usually continues running after a warning is generated.

A common example is including a file that does not exist:

<?php

include "config.php";

echo "Application continues running";

?>

If config.php is missing, PHP generates a warning, but the remaining code continues executing.

Warnings often indicate configuration problems or issues that should be fixed before they become bigger problems.

4. Notices

Notices are low-severity messages that indicate something unusual in your code. They usually do not stop script execution.

Accessing an undefined variable is a common example:

<?php

echo $username;

?>

If $username has not been defined, PHP generates a notice.

Although notices do not break your application, ignoring them can hide real coding mistakes. A variable that is unexpectedly empty may later cause incorrect output or logic errors.

5. Deprecated Warnings

Deprecated warnings indicate that a PHP feature or function still works but should not be used in new code because it may be removed in future PHP versions.

For example, older PHP applications may generate deprecated warnings after upgrading to a newer PHP version because they use outdated functions or syntax.

These warnings are especially useful when maintaining older applications. Fixing them helps keep your code compatible with future PHP releases.

6. User-Generated Errors

PHP allows developers to create custom errors using functions such as trigger_error().

This is useful when an application needs to report specific conditions that PHP cannot detect automatically.

<?php

$age = 15;

if ($age < 18) {
    trigger_error("User must be at least 18 years old.");
}

?>

Custom errors can make debugging easier by providing meaningful messages related to your application logic.

How to Display PHP Errors During Development

Displaying errors while developing a PHP application helps identify problems quickly. The recommended approach is to enable full error reporting in your development environment.

Add the following configuration at the beginning of your development scripts:

<?php

error_reporting(E_ALL);
ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');

?>

The E_ALL constant tells PHP to report all available error types.

The display_errors setting controls whether errors appear in the browser.

The display_startup_errors setting enables reporting of errors that happen while PHP starts loading.

For a complete application, it is usually better to configure these settings in php.ini instead of adding them to every PHP file.

Example development configuration:

display_errors = On
display_startup_errors = On
error_reporting = E_ALL

How to Log PHP Errors in Production

Displaying PHP errors on a live website is not recommended. Error messages can reveal internal details such as file paths, database information, and application logic.

Instead, production applications should log errors and review them privately.

A typical production configuration looks like this:

display_errors = Off
display_startup_errors = Off
log_errors = On
error_reporting = E_ALL

With this configuration:

  • Visitors will not see PHP error messages.
  • PHP will continue recording errors in the configured log file.
  • Developers can review logs to identify and fix problems.

The location of the PHP error log depends on your server configuration. You can check the configured location using the error_log setting in php.ini or by using the phpinfo() function.

You can check your active PHP configuration using the phpinfo() function.

For example:

error_log = /var/log/php_errors.log

When troubleshooting a production issue, checking the PHP error log is often the first place to look.

Handling PHP Errors with Custom Error Handlers

PHP allows developers to define custom error handlers using set_error_handler(). This gives more control over how errors are processed.

A custom handler can be useful when you want to:

  • Format errors consistently.
  • Send alerts to monitoring systems.
  • Store additional debugging information.
  • Convert certain errors into exceptions.

Example:

<?php

function customErrorHandler($severity, $message, $file, $line)
{
    echo "Error: " . $message;
    echo "<br>File: " . $file;
    echo "<br>Line: " . $line;
}

set_error_handler("customErrorHandler");

echo $undefinedVariable;

?>

PHP provides the set_error_handler() function to customize how errors are handled.

When the undefined variable is accessed, PHP calls the custom handler instead of using the default error output.

In real applications, avoid printing error details directly. A better approach is to log the information and show a user-friendly message.

PHP Errors vs Exceptions

PHP errors and exceptions both report problems, but they are used in different situations.

  • Errors are usually generated by PHP itself for problems such as syntax mistakes, missing files, or invalid operations.
  • Exceptions are objects that developers can throw and handle using try, catch, and finally.

For application-level problems, exceptions are usually preferred because they provide better control over error handling.

Example:

<?php

try {
    throw new Exception("Unable to process request.");
} catch (Exception $e) {
    echo $e->getMessage();
}

?>

Modern PHP applications commonly use exceptions for business logic errors while relying on PHP error reporting and logging for engine-level problems.

Common PHP Errors and How to Fix Them

Some PHP errors appear repeatedly during development. Knowing the common causes can save time when debugging.

Learn more about PHP exceptions in the official PHP exception documentation.

Undefined Variable

This error occurs when you try to use a variable that has not been initialized.

<?php

echo $name;

?>

Fix: Initialize variables before using them.

<?php

$name = "John";

echo $name;

?>

Call to Undefined Function

This happens when PHP cannot find the function you are trying to execute.

<?php

sendEmail();

?>

Fix: Check whether the function exists, whether the file containing the function is included, and whether the function name is spelled correctly.

Failed to Open Stream

This warning usually appears when PHP cannot access a file.

<?php

include "settings.php";

?>

Common causes include:

  • Incorrect file path.
  • Missing file.
  • Insufficient file permissions.

Fix: Verify the file path and use correct relative or absolute paths.

Headers Already Sent

This error occurs when PHP tries to send HTTP headers after output has already been sent to the browser.

Example:

<?php

echo "Hello";

header("Location: dashboard.php");

?>

Fix: Send headers before any output. Also check for accidental spaces or blank lines before the opening PHP tag.

Maximum Execution Time Exceeded

This error occurs when a script runs longer than the allowed execution time.

Common causes include:

  • Infinite loops.
  • Processing very large files.
  • Slow database queries.

Fix: Optimize the code, improve queries, or review the logic causing the delay instead of simply increasing the execution limit.

Best Practices for PHP Error Handling

Good error handling makes applications easier to maintain and troubleshoot.

  • Enable detailed errors during development: Developers should see complete error information while building an application.
  • Disable error display in production: Never expose internal errors to website visitors.
  • Always log errors: Logs provide valuable information when problems occur on live systems.
  • Use meaningful error messages: Messages should help developers identify the cause quickly.
  • Handle expected problems with exceptions: Use exceptions for application-level failures.
  • Monitor error logs regularly: Small warnings can become bigger problems if ignored.

A useful practice is to separate developer information from user messages. For example, instead of showing:

Database connection failed: Access denied for user 'admin'@'localhost'

show the visitor a simple message:

Something went wrong. Please try again later.

Then record the detailed error privately in the application logs.

Frequently Asked Questions About PHP Errors

How do I show all PHP errors?

Use error_reporting(E_ALL) to enable reporting for all PHP errors. During development, also enable error display:

<?php

error_reporting(E_ALL);
ini_set('display_errors', '1');

?>

For production websites, log errors instead of displaying them.

Why are PHP errors not showing?

PHP errors may not appear because error display is disabled in the PHP configuration.

Check these settings in your php.ini file:

display_errors = On
error_reporting = E_ALL

After changing php.ini, restart your web server for the changes to take effect.

Many PHP error reporting settings can be controlled through the php.ini configuration file.

Where are PHP errors stored?

PHP errors are stored in the configured error log file when log_errors is enabled.

You can find the configured location using the error_log setting in php.ini or by checking your server configuration.

Should PHP errors be displayed on a live website?

No. PHP errors should not be displayed publicly on production websites.

Error messages may reveal sensitive technical details. Enable logging instead and review the logs privately.

What is the difference between PHP errors and exceptions?

PHP errors are usually generated by the PHP engine, while exceptions are objects that developers can throw and handle in application code.

Modern PHP applications commonly use exceptions for expected application failures and error logging for unexpected runtime problems.

Conclusion

PHP errors are an important debugging tool during development and a valuable source of information in production when handled correctly.

Understanding PHP error types, enabling error reporting, and configuring proper logging will help you identify problems faster and build more reliable applications.

The goal is not to eliminate every error. Errors are part of development. The goal is to make them visible when you need them and invisible to users when you do not.

Photo of Vincy, PHP developer
Written by Vincy Last updated: July 22, 2026
I'm a PHP developer with 20+ years of experience and a Master's degree in Computer Science. I build and improve production PHP systems for eCommerce, payments, webhooks, and integrations, including legacy upgrades (PHP 5/7 to PHP 8.x).

Continue Learning

These related tutorials may help you continue learning.

6 Comments on "PHP Errors: Types, Reporting, and Error Handling"

Leave a Reply

Your email address will not be published. Required fields are marked *

Explore topics
Need PHP help?