简体   繁体   中英

PHP Output buffer

My own PHP framework parses files like this to grab the content:

ob_start();
include($file);
$content = ob_get_clean();

However, now I am working on an own error_handler to display an error page if any error occured even when it's happning in a template parsed like the code piece demonstrates above.

The funny thing is the content of the included template is printed to the browser when code execution dies due to an error for example. That of course makes my error-page looks very bad.

Shouldn't the opening of an output buffer prevent printing the content?

Especially because ob_get_clean() should clear the content after parsing it.

Why is this happening?

If I don't run into an error the website is working as intended.

How can I fix this problem?

I think I had the same problem. Try using ob_clean():

try {
    $app = new App($some_config);
    $app->run();
} 
catch (Exception $e) {
    ob_clean();
    // Display your error page.
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM