Home arrow PHP arrow Page 2 - Output Buffering With PHP

The Matrix Awaits - 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
Output control, you say? What's that? And more importantly, why do I care?

As the name suggests, PHP's output control functions provide a way for you, the developer, to exercise some degree of control over how the output generated by a particular PHP script is handled.

Why do you need this? Perhaps the best way to demonstrate is with an example.

<?php// send a headerheader ("Pragma: no-cache"); // send some outputprint "Welcome to the Matrix, Neo";// send another header// this will generate an errorheader ("Content-Type: text/html"); ?>
Here's what you'll see when you browse to the script:

Welcome to the Matrix, NeoWarning: Cannot add header information - headers already sent by (outputstarted at /usr/local/apache/htdocs/x1.php:7) in/usr/local/apache/htdocs/x1.php on line 11
In this case, PHP barfs because I've attempted to send a HTTP header to the browser after the script has generated a line of output. If you know anything about HTTP, you know that this is a big no-no - as the PHP manual succinctly puts it, "...you can not normally send headers to the browser after data has already been sent...".

So where does that leave you? Well, either you make it a point to ensure that all sensitive code - like the code that generates HTTP headers - always appears right at the top of your scripts...or you get creative with the output control functions available to you in PHP.

<?php// start buffering the outputob_start();// send a header header ("Pragma: no-cache"); // print some outputprint "Welcome to the Matrix, Neo";// send another header// in this case, since the output is being stored in a buffer // and notbeing printed, no error will be generated header ("Content-Type:text/html"); // print the contents of the bufferob_end_flush();?>
Now, if you run this script, you'll see the output

Welcome to the Matrix, Neo
with no nasty error messages spoiling the view.

How did this happen? Well, it's fairly simple. In this case, rather than having PHP send output directly to the standard output device (the browser) as the script gets executed, I've chosen to define a special output buffer which stores all the output generated by the script during its lifetime. When I do this, the output of the script is never seen by the user unless I explicitly make the contents of this buffer visible via a call to PHP's output control API (discussed in detail on the next page).

When you compare the two examples above, the advantage of using this technique should immediately become clear. By using a memory buffer to manage how the output of my script appears, I can do things which would otherwise be difficult or tedious - including sending HTTP headers to the browser *after* a script has started generating output, or (as you will see) displaying different Web pages on the basis of conditional tests within my script.

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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- 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...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: