3. Fatal Errors - example
<?php
//call a non-existent function
test_function();
?>
Output
Fatal error: Call to undefined function test_function() in
/var/www/website/html/index.php on line 4
4. Warnings and Notices
Undefined variables
Referencing non-existent array keys
Typos
Wrong variable types passed to functions
5. Warnings and Notices -
Example
<?php
//initialise the string variable
$string = ‘a string’;
//explode() a string
explode($string); ?>
Output:
Warning: Wrong parameter count for explode() in
/var/www/website.com/html/index.php on line 6
8. Warnings and Notices
Page will display, but may stop executing
prematurely
Error messages are hidden from the user,
but are logged
Affects performance & user experience
9. Database Errors
If the database can’t connect, it returns a
fatal error
If a table/field can’t or found, or a query can’t
be executed, it returns a warning
10. Better Error Handling
Define a custom error handler:
set_error_handler(‘function_name’);
Accept five arguments:-
• error type
• message
• file name
• line number
• context
11. Better Error Handling -
continued
Do whatever you want to within the function
– Output a message to the user
– Send an email to someone dependent on the
error
Errors are still logged
Should be built into base systems
Does not work for a fatal error.
12. Better Error Handling -
continued
register_shutdown_function()
This function will be called once a fatal error
has happened
However, it is executed after PHP has
finished processing every single script
Be careful with it!