Hate those ugly error messages that PHP generates when it encounters an error in your scripts? Can't stand half-constructed Web pages? Well, maybe you should take a look at PHP's output control functions, which offer an interesting and powerful solution to the problem.
Of course, there aren't just two functions available to you - the output control API has a few other cards up its sleeve as well. Consider the following example:
<?php// run at script startfunction page_init(){ // start buffering the output ob_start();}// run at script endfunction page_exit(){ global $error; // if an error occurred // erase all output generated so far // and display an error message if ($error == 1) { ob_end_clean(); print_error_template(); } // no errors? // display output else { ob_end_flush(); }}// print error pagefunction print_error_template(){ echo "<html><head><basefont face=Arial></head><body>An erroroccurred. Total system meltdown now in progress.</body></html>"; }page_init();// script code goes hereecho "This script is humming like a well-oiled machine";// if an error occurs// set the global $error variable to 1// uncomment the next line to see what happens if no error occurs $error= 1;page_exit();?>
In this case, depending on whether or not a particular error condition is met, PHP will either display the contents of the buffer, or wipe it clean via the ob_end_clean() function.