0% found this document useful (0 votes)
15 views

LPU Live Error Handling

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

LPU Live Error Handling

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 15

PHP Error Handling

• In PHP, error handling is crucial to building robust and


secure applications.

• PHP offers several ways to manage and respond to errors


in your code, ensuring that your application can handle
unexpected issues gracefully.

• Error handling in PHP is simple.

• An error message with filename, line number and a


message describing the error is sent to the browser.
Types of Errors
• Parse errors: Syntax errors in the script.
• Fatal errors: Errors that halt the script
execution, like calling a non-existent function.
• Warning errors: These do not stop script
execution but indicate something might be
wrong.
• Notice errors: Informative messages about
minor issues, like using an undefined variable.
different error handling methods:

• Simple "die()" statements

• Custom errors and error triggers

• Error reporting
Basic Error Handling: Using the die() function
• These functions can be used to stop script
execution if a critical error occurs e.g.
<?php
$file=fopen("mytestfile.txt","r");
?>
• But it will give an error,,,,if the file is not
present
Use Die function to prevent it
<?php
if(file_exists("mytestfile.txt")) {
$file = fopen("mytestfile.txt", "r");
} else {
die("Error: The file does not exist.");
}
?>
Custom Error Handler
• PHP allows you to define custom error-handling
functions using set_error_handler().

• This lets you handle errors in a more controlled


manner.

• We simply create a special function that can be


called when an error occurs in PHP.
• This function must be able to handle a
minimum of two parameters (error level and
error message) but can accept up to five
parameters (optionally: file, line-number, and
the error context):
Syntax

• error_function(error_level,error_message,
error_file,error_line,error_context)
Now lets create a function to handle errors:
function customError($errno, $errstr) {
echo "Error [$errno]: $errstr<br>";
}
set_error_handler("customError");
// Trigger an error
echo $undefined_var; // Will call customError
Testing the error handler by trying to output
variable that does not exist:
• <?php

function customError($errno, $errstr) {


echo "<b>Error:</b> [$errno] $errstr";
}

set_error_handler("customError");

//trigger error
echo($test);
?>
Trigger an Error

• In a script where users can input data it is


useful to trigger errors when an illegal input
occurs.

• In PHP, this is done by


the trigger_error() function.
For example
• In this example an error occurs if the "test"
variable is bigger than "1“
• <?php
$test=2;
if ($test>=1) {
trigger_error("Value must be 1 or below");
}
?>
• Possible error types:
• E_USER_ERROR - Fatal user-generated run-
time error. Errors that can not be recovered
from.
• E_USER_WARNING - Non-fatal user-generated
run-time warning.
• E_USER_NOTICE - Default. User-generated
run-time notice.
Error Logging

• PHP can log errors to a file instead of


displaying them to the user, which is helpful in
production environments.
• Configure error logging in the php.ini file:
log_errors = On
error_log = /path/to/error_log_file.log
• You can also log errors manually:
error_log("An error occurred", 3, "/path/to/custom_log.log");
<?php
function customError($errno, $errstr) {
echo "<b>Error:</b> [$errno] $errstr<br>";
echo "Webmaster has been notified";
error_log("Error: [$errno] $errstr",1,
"[email protected]","From:
[email protected]");
}
set_error_handler("customError",E_USER_WARNING);
$test=2;
if ($test>=1) {
trigger_error("Value must be 1 or
below",E_USER_WARNING);
}

You might also like