note 92016 modified in function.trigger-error by sobak
| From: | sobak@php.net | Date: | Thu, 24 Nov 2016 09:01:44 +0000 |
| Subject: | note 92016 modified in function.trigger-error by sobak | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-207546@lists.php.net to get a copy of this message | ||
trigger_error always reports the line and file that trigger_error was called on. Which isn't
very useful.
eg:
main.php:
<?php
include('functions.php');
$x = 'test';
doFunction($x);
?>
functions.php:
<?php
function doFunction($var) {
if(is_numeric($var)) {
/* do some stuff*/
} else {
trigger_error('var must be numeric');
}
}
?>
will output "Notice: var must be numeric in functions.php on line 6"
whereas "Notice: var must be numeric in main.php on line 4" would be more useful
here's a function to do that:
<?php
function error($message, $level=E_USER_NOTICE) {
$caller = next(debug_backtrace());
trigger_error($message.' in
<strong>'.$caller['function'].'</strong> called from
<strong>'.$caller['file'].'</strong> on line
<strong>'.$caller['line'].'</strong>'."\n<br />error
handler", $level);
}
?>
So now in our example:
main.php:
<?php
include('functions.php');
$x = 'test';
doFunction($x);
?>
functions.php:
<?php
function doFunction($var) {
if(is_numeric($var)) {
/* do some stuff*/
} else {
error('var must be numeric');
}
}
function error($message, $level=E_USER_NOTICE) {
$caller = next(debug_backtrace());
trigger_error($message.' in
<strong>'.$caller['function'].'</strong> called from
<strong>'.$caller['file'].'</strong> on line
<strong>'.$caller['line'].'</strong>'."\n<br />error
handler", $level);
}
?>
now outputs:
"Notice: var must be numeric in doFunction called from main.php on line 4"
--was--
trigger_error always reports the line and file that trigger_error was called on. Which isn't
very useful.
eg:
main.php:
<?php
include('functions.php');
$x = 'test';
doFunction($x);
?>
functions.php:
<?php
function doFunction($var) {
if(is_numeric($var)) {
/* do some stuff*/
} else {
trigger_error('var must be numeric');
}
}
?>
will output "Notice: var must be numeric in functions.php on line 6"
whereas "Notice: var must be numeric in main.php on line 4" would be more useful
here's a function to do that:
<?php
function error($message, $level=E_USER_NOTICE) {
$caller = next(debug_backtrace());
trigger_error($message.' in
<strong>'.$caller['function'].'</strong> called from
<strong>'.$caller['file'].'</strong> on line
<strong>'.$caller['line'].'</strong>'."\n<br />error
handler", $level);
}
?>
So now in our example:
main.php:
<?php
include('functions.php');
$x = 'test';
doFunction($x);
?>
functions.php:
<?php
function doFunction($var) {
if(is_numeric($var)) {
/* do some stuff*/
} else {
error('var must be numeric');
}
}
function error($message, $level=E_USER_NOTICE) {
$caller = next(debug_backtrace());
trigger_error($message.' in
<strong>'.$caller['function'].'</strong> called from
<strong>'.$caller['file'].'</strong> on line
<strong>'.$caller['line'].'</strong>'."\n<br />error
handler", $level);
}
?>
now outputs:
"Notice: var must be numeric in doFunction called from main.php on line 4"
http://www.puremango.co.uk
http://php.net/manual/en/function.trigger-error.php