Jump to content

ob_clean() - Trouble Understanding Code


Go to solution Solved by requinix,

Recommended Posts

Hello. I'm having a difficulty understanding the code below:

<?php
set_error_handler('errorHandler', E_ALL);

function errorHandler($number, $message, $file, $line){
	if(ob_get_length()) ob_clean();
	$errorMessage = 'Error: '.$number.'</br>'.
					'Message: '.$message.'</br>'.
					'File: '.$message.'</br>'.
					'Line: '.$message;
					
	echo $errorMessage;
}
?>

Specifically, the line 

if(ob_get_length()) ob_clean();

I've looked up what the function ob_clean() does, and I understand that it empties the output buffer, but I still don't see how it works within the code. I mean, what purpose would it serve to clean the buffer at the beginning of the function?

Link to comment
https://forums.phpfreaks.com/topic/278621-ob_clean-trouble-understanding-code/
Share on other sites

Based on my testing, I'm guessing that 

if(ob_get_length()) ob_clean();

will make sure that only the error gets outputted. 

 

Here's my test: 

<?php
 
set_error_handler('errorHandler', E_ALL);
 
echo "test";
 
throw Exception;
 
function errorHandler($number, $message, $file, $line) {
    if (ob_get_length())
        ob_clean();
    $errorMessage = 'Error: ' . $number . '</br>' .
            'Message: ' . $message . '</br>' .
            'File: ' . $message . '</br>' .
            'Line: ' . $message;
 
    echo $errorMessage;
}
?>
Edited by dannon

It makes sure that anything in the current output buffer (if anything) is cleared before the message is also added to the output buffer. It doesn't guarantee that the buffer (again, if present) won't get cleared later and the message disappears. This could make sense if whatever was generating output failed and the output can/should be discarded.

 

If you want to clear all output buffering to make sure that an error message is written then a better solution is

while (ob_get_level()) ob_end_clean(); // or ob_end_flush()
however that will cause problems if you use automatic output buffering (eg, with the output_buffering setting enabled). Edited by requinix

Alright. So ob_clean simply prevents content prior to the error from being displayed, correct? Hmmm, but I though I had to use ob_start() to active buffering, and it's not present anywhere in my code. Or is enabling it in the ini file enough? Is content inputed into the buffer regardless of whether I turn on buffering through the function?

  • Solution

You don't have to enable it. In fact unless you have a solid reason for doing so you should leave it disabled.

 

Yes, ob_clean() erases whatever is there. Yes, you have to call ob_start() to begin buffering, but with certain settings PHP will start one up automatically for you. My comment was merely that if you did so then the code I posted, which will work fine in most cases, would need an adjustment. It is also there to serve a different purpose than the code you actually have now, but what I believe was the intention.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.