Error Types and Error Reporting in PHP Practice Questions with Solutions

Introduction

Errors are a common part of PHP programming. They occur when code contains mistakes, uses invalid operations, or encounters unexpected situations. Understanding PHP error types and error reporting helps developers identify problems and fix them quickly. In this chapter, you will practise Error Types and Error Reporting in PHP handling common PHP errors and learn how to display, hide, and log error messages.

Question 1: Display an Error Message Using trigger_error()

Problem Statement

Create a PHP program that generates a user-defined warning when a student’s marks are below 40.

PHP Solution

<?php

$marks = 32;

if ($marks < 40) {
    trigger_error("Student has failed the examination.", E_USER_WARNING);
}

?>

Sample Output

Warning: Student has failed the examination.

Explanation

  • trigger_error() generates a user-defined error.
  • E_USER_WARNING creates a warning message.
  • The warning helps developers identify a condition that needs attention.

Concepts Covered

  • User-defined errors
  • trigger_error()
  • E_USER_WARNING

Question 2: Generate a User-Defined Notice

Problem Statement

Create a PHP program that generates a notice when an employee’s department is not assigned.

PHP Solution

<?php

$department = "";

if ($department == "") {
    trigger_error("Employee department is not assigned.", E_USER_NOTICE);
}

?>

Sample Output

Notice: Employee department is not assigned.

Explanation

  • E_USER_NOTICE generates a user-defined notice.
  • Notices indicate something that developers should review.
  • The program continues executing after generating a notice.

Concepts Covered

  • E_USER_NOTICE
  • User-defined notices
  • Error messages

Question 3: Display All PHP Errors Using error_reporting()

Problem Statement

Create a PHP program that enables reporting of all PHP errors, warnings, and notices.

PHP Solution

<?php

error_reporting(E_ALL);

$name = "Rahul";

echo $name;

?>

Sample Output

Rahul

Explanation

  • error_reporting() controls which PHP errors are reported.
  • E_ALL enables reporting of all supported error levels.
  • This setting is useful during development.

Concepts Covered

  • error_reporting()
  • E_ALL
  • PHP error reporting

Question 4: Hide Error Messages Using error_reporting()

Problem Statement

Create a PHP program that disables the reporting of PHP errors.

PHP Solution

<?php

error_reporting(0);

echo "Welcome to PHP Programming";

?>

Sample Output

Welcome to PHP Programming

Explanation

  • error_reporting(0) disables PHP error reporting.
  • Error messages are not displayed through normal PHP error reporting.
  • Disabling reporting does not fix errors.
  • During development, errors should remain enabled so problems can be identified.

Concepts Covered

  • Disabling error reporting
  • error_reporting(0)
  • Error visibility

Question 5: Display Errors Using ini_set()

Problem Statement

Create a PHP program that enables error display using ini_set().

PHP Solution

<?php

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

echo $studentName;

?>

Sample Output

Warning: Undefined variable $studentName

Explanation

  • ini_set() changes a PHP configuration setting at runtime when the setting allows it.
  • display_errors controls whether PHP errors are displayed.
  • error_reporting(E_ALL) enables reporting of all supported error levels.
  • The variable $studentName is not defined, so PHP reports a warning.

Concepts Covered

  • ini_set()
  • display_errors
  • Undefined variables

Question 6: Write Errors into a Log File

Problem Statement

Create a PHP program that saves error messages into a log file instead of displaying them on the screen.

PHP Solution

<?php

ini_set("log_errors", "1");
ini_set("error_log", "php_errors.log");

error_log("Database connection failed.");

echo "Error message saved in the log file.";

?>

Sample Output

Error message saved in the log file.

Log file content (php_errors.log):

Database connection failed.

Explanation

  • log_errors enables error logging.
  • error_log specifies the log file location.
  • error_log() writes a custom message to the configured error log.
  • The PHP process must have permission to write to the file.

Concepts Covered

  • error_log()
  • log_errors
  • PHP error log

Question 7: Understand a Fatal Error in PHP

Problem Statement

Create a PHP program that calls a function that does not exist and observe the resulting fatal error.

PHP Solution

<?php

echo "Program started.";

calculateSalary();

echo "Program completed.";

?>

Sample Output

Program started.

Fatal error: Uncaught Error: Call to undefined function calculateSalary()

Explanation

  • PHP tries to execute calculateSalary().
  • The function has not been defined.
  • PHP generates an Error and stops normal execution.
  • The final echo statement is not executed.

Concepts Covered

  • Fatal errors
  • Undefined functions
  • PHP Error

Question 8: Handle an Exception Using try and catch

Problem Statement

Create a PHP program that divides two numbers and handles a division-by-zero exception.

PHP Solution

<?php

$num1 = 100;
$num2 = 0;

try {

    if ($num2 == 0) {
        throw new Exception("Division by zero is not allowed.");
    }

    echo $num1 / $num2;

} catch (Exception $e) {

    echo "Error: " . $e->getMessage();

}

?>

Sample Output

Error: Division by zero is not allowed.

Explanation

  • try contains the code that may produce an exception.
  • throw creates an exception.
  • catch handles the exception.
  • getMessage() returns the exception message.
  • The program continues after the exception is handled.

Concepts Covered

  • Exceptions
  • try
  • catch
  • throw
  • getMessage()

Question 9: Create a Custom Error Handler

Problem Statement

Create a PHP program that uses a custom error handler to display a message when a warning occurs.

PHP Solution

<?php

function customErrorHandler($errno, $errstr) {

    echo "Custom Error Handler: " . $errstr;

}

set_error_handler("customErrorHandler");

trigger_error("Invalid student information.", E_USER_WARNING);

?>

Sample Output

Custom Error Handler: Invalid student information.

Explanation

  • customErrorHandler() is a user-defined function.
  • $errno receives the error level.
  • $errstr receives the error message.
  • set_error_handler() registers the custom error handler.
  • When the warning occurs, the custom handler processes it.

Concepts Covered

  • set_error_handler()
  • Custom error handling
  • Error level and message

Question 10: Build a PHP Error Logging System

Problem Statement

Create a simple employee registration program that checks employee information and saves an error message to a log file if the employee name is empty.

PHP Solution

<?php

ini_set("log_errors", "1");
ini_set("error_log", "employee_errors.log");

$employeeName = "";
$salary = 30000;

if ($employeeName == "") {

    error_log("Employee name is missing.");

    echo "Employee registration failed.";

} else {

    echo "Employee registered successfully.";

}

?>

Sample Output

Employee registration failed.

Log file content (employee_errors.log):

Employee name is missing.

Explanation

  • The program checks whether the employee name is empty.
  • If the name is missing, error_log() saves the message.
  • Otherwise, the program displays a successful registration message.
  • This approach helps developers investigate problems without displaying technical details to website visitors.

Concepts Covered

  • Error logging
  • error_log()
  • Conditional error checking
  • Practical error management

Key Takeaways

  • PHP errors help developers identify problems in programs.
  • error_reporting() controls which error levels are reported.
  • E_ALL enables reporting of all supported error levels.
  • ini_set() can change certain PHP configuration settings at runtime.
  • trigger_error() generates user-defined errors.
  • error_log() saves messages in an error log.
  • try, catch, and throw are used to handle exceptions.
  • set_error_handler() allows developers to create custom error handlers.
  • Error reporting should be enabled during development.
  • On production websites, detailed errors should generally be logged rather than displayed to visitors.

Frequently Asked Questions (FAQs)

1. What are errors in PHP?

PHP errors are problems that occur while writing or executing PHP code. They help developers identify and fix issues in their programs.

2. What are the main types of PHP errors?

Common PHP error categories include notices, warnings, fatal errors, parse errors, and exceptions. PHP also provides different error levels for reporting.

3. What is error_reporting() in PHP?

error_reporting() is a PHP function that controls which types of errors are reported.

4. What is E_ALL in PHP?

E_ALL is a PHP error-reporting level that enables reporting of all supported error levels.

5. What is the difference between a warning and a fatal error?

A warning generally allows the script to continue executing, while a fatal error can stop normal script execution.

6. What is trigger_error() in PHP?

trigger_error() generates a user-defined error or notice in a PHP program.

7. What is error_log() in PHP?

error_log() sends an error message to the configured PHP error log or another specified destination.

8. What is set_error_handler() in PHP?

set_error_handler() registers a custom function to handle supported PHP errors.

9. What is the difference between an error and an exception?

An error is a problem reported by PHP, while an exception is an object that can be thrown and handled using try and catch. PHP also provides throwable error objects.

10. Should PHP errors be displayed on a live website?

Generally, detailed errors should not be displayed to visitors. Developers should log errors and keep sensitive technical information private.

Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.

Scroll to Top