note 113379 added to function.ob-start

From: Date: Thu, 03 Oct 2013 20:23:14 +0000
Subject: note 113379 added to function.ob-start
Groups: php.notes 
Request: Send a blank email to php-notes+get-196459@lists.php.net to get a copy of this message
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); ?> ---- Server IP: 69.147.83.199 Probable Submitter: 108.183.129.223 ---- Manual Page -- http://php.net/manual/en/function.ob-start.php Edit -- https://master.php.net/note/edit/113379 Del: integrated -- https://master.php.net/note/delete/113379/integrated Del: useless -- https://master.php.net/note/delete/113379/useless Del: bad code -- https://master.php.net/note/delete/113379/bad+code Del: spam -- https://master.php.net/note/delete/113379/spam Del: non-english -- https://master.php.net/note/delete/113379/non-english Del: in docs -- https://master.php.net/note/delete/113379/in+docs Del: other reasons-- https://master.php.net/note/delete/113379 Reject -- https://master.php.net/note/reject/113379 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#196459) next »