Home arrow PHP arrow Page 4 - Output Buffering With PHP

Melting Down - PHP

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.

TABLE OF CONTENTS:
  1. Output Buffering With PHP
  2. The Matrix Awaits
  3. Start Me Up
  4. Melting Down
  5. Today's Forecast
  6. Making It Simpler
  7. The Real World
  8. Zip Zap Zoom
  9. Endgame
By: Harish Kamath, (c) Melonfire
Rating: starstarstarstarstar / 69
April 30, 2002

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement
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.

 
 
>>> More PHP Articles          >>> More By Harish Kamath, (c) Melonfire
 

blog comments powered by Disqus
   

PHP ARTICLES

- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...
- PHP: Building Concrete Validators
- Sanitizing Input with PHP
- Executing Shell Commands with PHP
- Handling File Data with PHP
- File Security and Resources with PHP
- ArrayObject PHP Class Examples
- ArrayObject PHP Class: An Introduction
- Getting File System Data with PHP
- PHP Tools for Working with the File and Oper...
- Working with the File and Operating System w...
- PHP Proxy Patterns: Completing a Blog


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 3 - Follow our Sitemap

Dev Shed Tutorial Topics: