logPHP
The code is an example of custom error handling in PHP that logs errors to a file. It defines a custom error handling function called customError() which will be called when an error occurs. The function takes two parameters: $errno, which is the error number, and $errstr, which is the error message.
The function then uses the error_log() function to log the error message to a file called "error.log". The error_log() function takes three parameters: the message, the message type, and the destination. In this case, the message is "Error: [$errno] $errstr", the message type is 3, which means that the message is being logged to a file and the destination is "error.log".
The script also sets this custom error handler function to be used with the set_error_handler() function. This function takes two parameters: the name of the function to be used as the error handler and the error level to be handled by the error handler. In this case, it is set to handle E_WARNING level errors.
After this, the code triggers an error by calling a require statement to include a file that does not exist, 'fileDoesNotExist.txt', this will throw an error because the file does not exist. The error handler function customError() will be called, it will log the error message "fileDoesNotExist.txt" to the "error.log" file and also display the message 'Error: 2 fileDoesNotExist.txt' to the browser.
This method allows you to keep track of errors that occur on a live website, and can also be useful for debugging. You can also send an email to the admin when an error occurs by using the same error_log function by providing the email address as the destination.
<?php
// you cam also log php errors to a file, when they happen
// this is useful when you have a live website and you want to keep track of errors
// you can also send an email to the admin when an error occurs
// this is done by using the error_log() function
// the error_log() function sends an error to a file or email
// this function will be called when an error occurs
function customError($errno, $errstr) {
// this will log the error to a file
error_log("Error: [$errno] $errstr", 3, "error.log");
echo "Error: [$errno] $errstr";
}
// set error handler
set_error_handler("customError", E_WARNING);
// trigger error on false require
require ('fileDoesNotExists.txt'); // this will throw an error because the file does not exist