BorysSokolov Posted May 31, 2013 Share Posted May 31, 2013 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? Quote Link to comment https://forums.phpfreaks.com/topic/278621-ob_clean-trouble-understanding-code/ Share on other sites More sharing options...
dannon Posted May 31, 2013 Share Posted May 31, 2013 (edited) 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 May 31, 2013 by dannon Quote Link to comment https://forums.phpfreaks.com/topic/278621-ob_clean-trouble-understanding-code/#findComment-1433323 Share on other sites More sharing options...
requinix Posted May 31, 2013 Share Posted May 31, 2013 (edited) 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 May 31, 2013 by requinix Quote Link to comment https://forums.phpfreaks.com/topic/278621-ob_clean-trouble-understanding-code/#findComment-1433334 Share on other sites More sharing options...
BorysSokolov Posted May 31, 2013 Author Share Posted May 31, 2013 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? Quote Link to comment https://forums.phpfreaks.com/topic/278621-ob_clean-trouble-understanding-code/#findComment-1433342 Share on other sites More sharing options...
Solution requinix Posted May 31, 2013 Solution Share Posted May 31, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/278621-ob_clean-trouble-understanding-code/#findComment-1433355 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.