PHP error handling functions - set_error_handler()
Description
The set_error_handler() function is used to set a user-defined error handling function.
Version:
PHP 4.0.1, PHP 5
Syntax:
set_error_handler (error_handler, error_types)
Parameter:
Parameters | Description | Required / Optional | Type |
---|---|---|---|
error_handler | Specifies the function to be run at errors. Contains : errorno - An integer indicating the level of the error. errstr - A string which contains the error message. errfile - A string which contains the filename that the error was raised in. This is optional. errline - An integer indicating the line number the error was raised at. This is optional. errcontext - This is an array which contains all the variables that existed in the scope the error was triggered in. This is optional. | Required | callback |
error_types | Purpose of this parameter is to mask the triggering of the error_handler function resembling the error_reporting ini setting controls which are to be shown. If not used, error_handler is called for every error irrespective of the setting of the error_reporting setting. | Required | integer |
Return Values
The function returns a string which contains the previously defined error handler. But it returns NULL if the built-in error handler is used instead of a user defined one. In the case of an error resembling to invalid callback, the function returns NULL. If the user defined error handler used previously was a class method, the function returns an indexed array containing the name of the class and method.
Example :
<?php
function w3r_notice($no, $str, $file, $line) {
echo "Encountered notice $no in $file, line $line: $str\n";
}
function func_error($num, $str, $file, $line) {
echo "Encountered error $num in $file, line $line: $str\n";
}
set_error_handler("w3r_notice", E_NOTICE);
set_error_handler("w3r_error", E_ERROR);
echo $demo;
?>
Previous: restore_exception_handler()
Next: set_exception_handler()
PHP: Tips of the Day
How to Sort Multi-dimensional Array by Value?
Try a usort, If you are still on PHP 5.2 or earlier, you'll have to define a sorting function first:
Example:
function sortByOrder($a, $b) { return $a['order'] - $b['order']; } usort($myArray, 'sortByOrder');
Starting in PHP 5.3, you can use an anonymous function:
usort($myArray, function($a, $b) { return $a['order'] - $b['order']; });
And finally with PHP 7 you can use the spaceship operator:
usort($myArray, function($a, $b) { return $a['order'] <=> $b['order']; });
To extend this to multi-dimensional sorting, reference the second/third sorting elements if the first is zero - best explained below. You can also use this for sorting on sub-elements.
usort($myArray, function($a, $b) { $retval = $a['order'] <=> $b['order']; if ($retval == 0) { $retval = $a['suborder'] <=> $b['suborder']; if ($retval == 0) { $retval = $a['details']['subsuborder'] <=> $b['details']['subsuborder']; } } return $retval; });
If you need to retain key associations, use uasort() - see comparison of array sorting functions in the manual
Ref : https://bit.ly/3i77vCC
- New Content published on w3resource:
- HTML-CSS Practical: Exercises, Practice, Solution
- Java Regular Expression: Exercises, Practice, Solution
- Scala Programming Exercises, Practice, Solution
- Python Itertools exercises
- Python Numpy exercises
- Python GeoPy Package exercises
- Python Pandas exercises
- Python nltk exercises
- Python BeautifulSoup exercises
- Form Template
- Composer - PHP Package Manager
- PHPUnit - PHP Testing
- Laravel - PHP Framework
- Angular - JavaScript Framework
- Vue - JavaScript Framework
- Jest - JavaScript Testing Framework