note 113379 deleted from function.ob-start by cmb

From: Date: Sun, 14 Jul 2019 11:22:57 +0000
Subject: note 113379 deleted from function.ob-start by cmb
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-211569@lists.php.net to get a copy of this message
Note Submitter: David Spector ---- Function ob_start can capture all normal output, assuming no errors occur. Naturally, the programmer will want to know whether errors can also be caught (that is, captured and logged). The answer is that warnings can be caught along with normal output, but that fatal errors and exceptions break the normal buffering and can only be caught separately. Finally, parsing errors cannot be caught at all, because parsing is done in a first pass before execution begins. Of course, even parsing errors can be caught by running PHP in a separate process and capturing the output of that process. Here is an example that illustrates all these additional details (see also the comment by Philip at set_error_handler function). Those errors that break buffering can be illustrated by uncommenting their lines. <?php ob_start(); // Start output buffering function exception_handler($exc) { // Catch thrown exceptions break output buffering exit("Uncaught exception: ".$exc->getMessage()); } // exception_handler set_exception_handler('exception_handler'); function shutdown_function() { // Catch fatal errors that break output buffering $error=error_get_last(); if ($error["type"]==E_ERROR) echo "Uncaught fatal error: ".$error["message"]; } // shutdown_function register_shutdown_function("shutdown_function"); // Demonstrate normal output echo 'This is normal output, caught okay'; // Demonstrate a warning $DivisionByZeroWarningCaughtOkay=3/0; // Demonstrate a fatal error //UndefinedFunctionFatalErrorCanBeCaughtSeparately(); // Demonstrate a parsing error //$x=FatalParse@ErrorCannotBeCapturedAtAll // Complete capturing normal output $Out=ob_get_contents(); ob_end_clean(); // Demonstrate a thrown exception //throw new Exception('Exception Can Be Caught Separately'); echo "All captured output:<p>".$Out; exit(0); ?>

« previous php.notes (#211569) next »