Page 1 of 1

Assign PHP error to variable like mysql_error()?

Posted: Fri Aug 08, 2008 1:00 am
by JAB Creations
I find it very useful to be able to handle MySQL errors in the following manner...

Code: Select all

$_SESSION['status'] = mysql_error();
I'm wondering if I can do the same with PHP error messages?

Re: Assign PHP error to variable like mysql_error()?

Posted: Fri Aug 08, 2008 2:16 am
by JAB Creations
Thanks to Ollie for pointing me in the right direction by pointing me to set_error_handler.

My main goal is to actually hold the error and echo it out somewhere else in the (X)HTML output. For example on my website if a PHP error occurs I can't view it because it's obstructed by positioned elements. Having errors echoed elsewhere would make it easier for me to deal with them.

I've been messing with this bit below without success though I still need to read the page a little more thoroughly.

Code: Select all

<?php
function error_handler($output)
{
  $error = error_get_last();
  $output = "";
  foreach ($error as $info => $string)
    $output .= "{$info}: {$string}\n";
    return $output;
}
 
ob_start('error_handler');
 
_1_will_this_undefined_function_raise_an_error();
_2_will_this_undefined_function_raise_an_error();
?>
 
<div>
<?php
// I want to echo the error message here.
?>
</div>