Why ob_get_clean() does not work in body of a function?
I'm using the code below for logging and sending error to my e-mail, but i don't know why $content
variable does not contain anything when i check my emails. Is this a scope mistake? or I'm doing something wrong?
ob_start();
set_error_handler('cs_handler', E_ALL);
//a lot includes and method calls here
function cs_handler($errno, $errstr, $errfile, $errline)
{
$content = ob_get();
mail(...., 'Error Happend: '.$content);
}
开发者_如何转开发
One issue is that you'll need to call ob_start()
again, after flushing the buffer, if you want multiple errors-per-page to work properly. Could this be the problem? Are you getting one email with content, followed by others without?
Another issue you might be having is that you're calling mail()
with 2 arguments, when it expects 3 (address, subject, content). You probably want something like:
mail('you@example.com', 'Error Happened', $content);
Note that the following works as expected:
ob_start();
set_error_handler('cs_handler', E_ALL);
echo 'begun';
echo $arr['test']; // This throws a warning, handled by the function below
function cs_handler($errno, $errstr, $errfile, $errline)
{
$content = ob_get_clean();
mail('you@example.com', 'Error Happened', $content);
}
精彩评论