note 92016 added to function.trigger-error

From: Date: Sun, 05 Jul 2009 11:39:59 +0000
Subject: note 92016 added to function.trigger-error
Groups: php.notes 
Request: Send a blank email to php-notes+get-157937@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" http://www.puremango.co.uk ---- Server IP: 92.48.74.199 Probable Submitter: 87.194.252.116 ---- Manual Page -- http://www.php.net/manual/en/function.trigger-error.php Edit -- https://master.php.net/note/edit/92016 Del: integrated -- https://master.php.net/note/delete/92016/integrated Del: useless -- https://master.php.net/note/delete/92016/useless Del: bad code -- https://master.php.net/note/delete/92016/bad+code Del: spam -- https://master.php.net/note/delete/92016/spam Del: non-english -- https://master.php.net/note/delete/92016/non-english Del: in docs -- https://master.php.net/note/delete/92016/in+docs Del: other reasons-- https://master.php.net/note/delete/92016 Reject -- https://master.php.net/note/reject/92016 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#157937) next »