Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Вивчайте Error Levels and Reporting | PHP Error Types and Reporting
Practice
Projects
Quizzes & Challenges
Вікторини
Challenges
/
PHP Error Handling

bookError Levels and Reporting

To handle errors effectively in PHP, you need to understand the different error levels and how to control which errors are reported. PHP categorizes errors into several types, each represented by a constant and a numeric value. Here are the most common error levels you will encounter:

  • E_ERROR (1): Fatal runtime errors that stop script execution;
  • E_WARNING (2): Non-fatal runtime warnings that do not stop script execution;
  • E_PARSE (4): Compile-time parse errors;
  • E_NOTICE (8): Minor run-time notices about possible issues in your code;
  • E_CORE_ERROR (16): Fatal errors that occur during PHP's initial startup;
  • E_CORE_WARNING (32): Warnings that occur during PHP's initial startup;
  • E_COMPILE_ERROR (64): Fatal compile-time errors;
  • E_COMPILE_WARNING (128): Compile-time warnings;
  • E_USER_ERROR (256): User-generated error messages;
  • E_USER_WARNING (512): User-generated warning messages;
  • E_USER_NOTICE (1024): User-generated notice messages;
  • E_STRICT (2048): Suggestions for best practices and code improvement;
  • E_RECOVERABLE_ERROR (4096): Catchable fatal errors;
  • E_DEPRECATED (8192): Warnings about deprecated features;
  • E_USER_DEPRECATED (16384): User-generated deprecation warnings.

These constants can be combined using the bitwise OR operator to control which errors are reported. By default, PHP may not display all error types, especially in production environments.

error_reporting_demo.php

error_reporting_demo.php

copy
123456789101112131415161718192021222324
<?php // Toggle between two error reporting modes $showAllErrors = true; if ($showAllErrors) { // Show all errors, warnings, and notices error_reporting(E_ALL); ini_set('display_errors', '1'); echo "All errors will be shown.<br>"; } else { // Show only critical errors error_reporting(E_ERROR | E_PARSE); ini_set('display_errors', '1'); echo "Only critical errors will be shown.<br>"; } // Trigger different types of errors echo $undefinedVar; // E_NOTICE: undefined variable include('nonexistentfile.php'); // E_WARNING: file not found // Uncomment to trigger a fatal error // undefinedFunction(); // E_ERROR: call to undefined function ?>

In the code above, you see how error_reporting and ini_set work together to control which errors are displayed. When $showAllErrors is set to true, error_reporting(E_ALL) enables reporting for every error level, and ini_set('display_errors', '1') ensures that these errors are actually shown in the browser. This means notices, warnings, and errors will all be visible as you run the script. For instance, using an undefined variable triggers an E_NOTICE, and including a nonexistent file triggers an E_WARNING.

When $showAllErrors is set to false, only critical errors (E_ERROR and E_PARSE) are reported. Less severe problems like notices and warnings are ignored and not shown, even if they occur. This is useful in production to avoid exposing sensitive information or overwhelming the output with minor issues.

By changing the values passed to error_reporting, you control which error levels are reported. Adjusting ini_set('display_errors', '1') or '0' controls whether these errors are actually displayed to the user. This flexibility allows you to tailor error reporting to your environment, such as showing all errors during development and only critical errors in production.

question mark

Which PHP function or directive allows you to change which error levels are reported at runtime?

Select the correct answer

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 1. Розділ 2

Запитати АІ

expand

Запитати АІ

ChatGPT

Запитайте про що завгодно або спробуйте одне із запропонованих запитань, щоб почати наш чат

bookError Levels and Reporting

Свайпніть щоб показати меню

To handle errors effectively in PHP, you need to understand the different error levels and how to control which errors are reported. PHP categorizes errors into several types, each represented by a constant and a numeric value. Here are the most common error levels you will encounter:

  • E_ERROR (1): Fatal runtime errors that stop script execution;
  • E_WARNING (2): Non-fatal runtime warnings that do not stop script execution;
  • E_PARSE (4): Compile-time parse errors;
  • E_NOTICE (8): Minor run-time notices about possible issues in your code;
  • E_CORE_ERROR (16): Fatal errors that occur during PHP's initial startup;
  • E_CORE_WARNING (32): Warnings that occur during PHP's initial startup;
  • E_COMPILE_ERROR (64): Fatal compile-time errors;
  • E_COMPILE_WARNING (128): Compile-time warnings;
  • E_USER_ERROR (256): User-generated error messages;
  • E_USER_WARNING (512): User-generated warning messages;
  • E_USER_NOTICE (1024): User-generated notice messages;
  • E_STRICT (2048): Suggestions for best practices and code improvement;
  • E_RECOVERABLE_ERROR (4096): Catchable fatal errors;
  • E_DEPRECATED (8192): Warnings about deprecated features;
  • E_USER_DEPRECATED (16384): User-generated deprecation warnings.

These constants can be combined using the bitwise OR operator to control which errors are reported. By default, PHP may not display all error types, especially in production environments.

error_reporting_demo.php

error_reporting_demo.php

copy
123456789101112131415161718192021222324
<?php // Toggle between two error reporting modes $showAllErrors = true; if ($showAllErrors) { // Show all errors, warnings, and notices error_reporting(E_ALL); ini_set('display_errors', '1'); echo "All errors will be shown.<br>"; } else { // Show only critical errors error_reporting(E_ERROR | E_PARSE); ini_set('display_errors', '1'); echo "Only critical errors will be shown.<br>"; } // Trigger different types of errors echo $undefinedVar; // E_NOTICE: undefined variable include('nonexistentfile.php'); // E_WARNING: file not found // Uncomment to trigger a fatal error // undefinedFunction(); // E_ERROR: call to undefined function ?>

In the code above, you see how error_reporting and ini_set work together to control which errors are displayed. When $showAllErrors is set to true, error_reporting(E_ALL) enables reporting for every error level, and ini_set('display_errors', '1') ensures that these errors are actually shown in the browser. This means notices, warnings, and errors will all be visible as you run the script. For instance, using an undefined variable triggers an E_NOTICE, and including a nonexistent file triggers an E_WARNING.

When $showAllErrors is set to false, only critical errors (E_ERROR and E_PARSE) are reported. Less severe problems like notices and warnings are ignored and not shown, even if they occur. This is useful in production to avoid exposing sensitive information or overwhelming the output with minor issues.

By changing the values passed to error_reporting, you control which error levels are reported. Adjusting ini_set('display_errors', '1') or '0' controls whether these errors are actually displayed to the user. This flexibility allows you to tailor error reporting to your environment, such as showing all errors during development and only critical errors in production.

question mark

Which PHP function or directive allows you to change which error levels are reported at runtime?

Select the correct answer

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 1. Розділ 2
some-alt