Assign PHP error to variable like mysql_error()?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Assign PHP error to variable like mysql_error()?

Post 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?
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

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

Post 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>
Post Reply