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

05 - Error

The document outlines PHP error handling, emphasizing its importance in maintaining application stability, improving user experience, and aiding debugging. It categorizes errors into informational, actionable, and fatal types, and discusses how to identify and customize error handling through various PHP functions. Additionally, it covers the creation of custom error handlers to manage errors effectively in scripts.

Uploaded by

ahraar278
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

05 - Error

The document outlines PHP error handling, emphasizing its importance in maintaining application stability, improving user experience, and aiding debugging. It categorizes errors into informational, actionable, and fatal types, and discusses how to identify and customize error handling through various PHP functions. Additionally, it covers the creation of custom error handlers to manage errors effectively in scripts.

Uploaded by

ahraar278
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 28

Islamic Republic of Afghanistan

Ministry of Higher Education


Herat University
Computer Science Faculty

Web Engineering (Semester 7)


PHP Error Handling
Lecture 05

Lecturer: Ahmad Ershad Omari


[email protected]

PHP Error Handling


Importance of Error Handling

• Prevents Unexpected Failures: Proper handling ensures your


application remains stable.
• Improves User Experience: Informative error messages guide users
rather than leaving them confused.
• Eases Debugging: Clear error reporting aids developers in quickly
identifying issues.
Types of Errors

There are 12 unique error types, which can


be grouped into 3 main categories:
• Informational (Notices)
• Actionable (Warnings)
• Fatal
Informational Errors

• Harmless problem, and can be avoided through use of explicit


programming.

e.g. use of an undefined variable, defining a string without quotes, etc.

See class example error1.php


Actionable Errors

• Indicate that something clearly wrong has happened and that action
should be taken.

e.g. file not present, undefined property of a class, etc.

See class example error2.php


Fatal Errors

• Something so terrible has happened during execution of your script


that further processing simply cannot continue.

e.g. parsing error, calling an undefined function, etc.

See class example error3.php


Identifying Errors

Error Type Description Example


Notices about possible Using an uninitialized
E_NOTICE
issues variable
Runtime warnings that
E_WARNING don’t stop script File inclusion failure
execution
Critical errors
Calling a non-existent
E_ERROR stopping script
function
execution

User-triggered fatal trigger_error("Fatal Error",


E_USER_ERROR
error E_USER_ERROR);
Identifying Errors
E_STRICT Feature or behaviour is depreciated (PHP5).
E_NOTICE Detection of a situation that could indicate a problem, notice
but might be normal.
E_USER_NOTICE User triggered notice.
E_WARNING Actionable error occurred during execution.
E_USER_WARNING User triggered warning. warning
E_COMPILE_WARNING Error occurred during script compilation (unusual)
E_CORE_WARNING Error during initialization of the PHP engine.
E_ERROR Unrecoverable error in PHP code execution.
E_USER_ERROR User triggered fatal error.
E_COMPILE_ERROR Critical error occurred while trying to read script.
fatal
E_CORE_ERROR Occurs if PHP engine cannot startup/etc.
E_PARSE Raised during compilation in response to syntax error
Causing errors or User Defined Error

• It is possible to cause PHP at any point in your script.


trigger_error($msg, $type);
e.g.

if (!$db_conn) {
trigger_error(‘db conn failed’, E_USER_ERROR);
}

PHP
Error
Handling
Customizing Error Handling

• Generally, how PHP handles errors is defined by various constants in


the installation (php.ini).
• There are several things you can control in your scripts however..
1. Set error reporting settings

error_reporting($level)

This function can be used to control which errors are displayed, and
which are simply ignored. The effect only lasts for the duration of the
execution of your script.
1. Set error reporting settings
<?php
// Turn off all error reporting
error_reporting(0);

// Report simple running errors


error_reporting(E_ERROR | E_WARNING | E_PARSE);

// Reporting E_NOTICE can be good too (to report uninitialized


// variables or catch variable name misspellings ...)
error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE);

// Report all errors except E_NOTICE


error_reporting(E_ALL ^ E_NOTICE);
error_reporting(E_ALL & ~ E_NOTICE);

// Report ALL PHP errors


error_reporting(E_ALL);
?>
See class example error4.php
1. Set error reporting settings

• Hiding errors is NOT a solution to a problem.

• It is useful, however, to hide any errors produced on a live server.


• While developing and debugging code, displaying all errors is highly
recommended!
2. Suppressing Errors

• The special @ operator can be used to suppress function errors.


• Any error produced by the function is suppressed and not displayed
by PHP regardless of the error reporting setting.
2. Suppressing Errors

$db = @mysqli_connect($h,$u,$p);
if (!$db) {
trigger_error(‘blah’,E_USER_ERROR);
}
2. Suppressing Errors

$db
$db == @mysqli_connect($h,$u,$p);
@mysql_connect($h,$u,$p);
if (!$db) {
trigger_error(blah.',E_USER_ERROR);
}
Attempt to connect to
database. Suppress error
notice if it fails..
Since error is suppressed, it
2. Suppressing Errors
must be handled gracefully
somewhere else..

$db = @mysql_connect($h,$u,$p);
if (!$db)
if (!$db) {{
trigger_error(‘blah’,E_USER_ERROR);
trigger_error(blah.',E_USER_ERROR);
}}
2. Suppressing Errors

• Error suppression is NOT a solution to a problem.

• It can be useful to locally define your own error handling


mechanisms.
• If you suppress any errors, you must check for them yourself
elsewhere.
3. Custom Error Handler

• You can write your own function to handle PHP errors in any way you
want.
• You simply need to write a function with appropriate inputs, then
register it in your script as the error handler.
• The handler function should be able to receive 4 arguments, and
return true to indicate it has handled the error…
3. Custom Error Handler

function err_handler(
$errcode,$errmsg,$file,$lineno) {

echo ‘An error has occurred!<br />’;


echo “file: $file<br />”;
echo “line: $lineno<br />”;
echo “Problem: $errmsg”;
return true;
}
3. Custom Error Handler

function err_handler(
$errcode,$errmsg,$file,$lineno) {
$errcode,$errmsg,$file,$lineno) {

The handler must have 4 inputs..


echo ‘An error has occurred!<br />’;
1.error code
echo “file: $file<br />”;
2.error
echo message
“line: $lineno<br />”;
3.file
echo where error
“Problem: occurred
$errmsg”;
4.linetrue;
return at which error occurred
}
3. Custom Error Handler

function err_handler(
$errcode,$errmsg,$file,$lineno) {

echo
echo ‘An
‘An error
error has
has occurred!<br
occurred!<br />’;
/>’;
echo
echo“file:
“file:$file<br
$file<br/>”;
/>”;
echo “line: $lineno<br />”;
echo “line: $lineno<br />”;
echo “Problem: $errmsg”;
echo “Problem: $errmsg”;
returnAny
true;
PHP statements can be
}
executed…
3. Custom Error Handler

function err_handler(
$errcode,$errmsg,$file,$lineno) {
Return true to let PHP know
echo ‘An error has occurred!<br />’;
that the custom error handler
echo “file: $file<br />”;
has handled the error
echo “line: $lineno<br />”;
OK.
echo “Problem: $errmsg”;
return true;
return true;
}
3. Custom Error Handler
• The function then needs to be registered as your custom error handler:
set_error_handler(‘err_handler’);
• You can ‘mask’ the custom error handler so it only receives certain types
of error. e.g. to register a custom handler just for user triggered errors:
set_error_handler(‘err_handler’,
E_USER_NOTICE | E_USER_WARNING |
E_USER_ERROR);
3. Custom Error Handler

• A custom error handler is never passed E_PARSE,


E_CORE_ERROR or E_COMPILE_ERROR errors as these are
considered too dangerous.
• Often used in conjunction with a ‘debug’ flag for neat combination of
debug and production code display..

See class example error5.php


Review
• Various different error types exist in PHP.
• The error handling system is highly flexible, and your own error handling
methods can be developed.
28

You might also like