Microsoft Word - Core PHP Programming Using PHP to Build Dynamic Web Sites

(singke) #1

?>


Of course, anything outside PHP tags is sent directly to the browser. This is undoubtedly
the fastest and least flexible way to send content. You might wonder at this point when
it's appropriate to use print and when you should place text outside PHP tags. There are
issues of efficiency and readability to worry about, but put them aside for now. The final
section of the book deals with this issue at length.


Output Buffering


As stated above, the Web server buffers content sent to the browser, and you can request
that the buffer be flushed. PHP4 introduced a new mechanism for buffering output you
can control completely. Four functions control PHP's output buffer: ob_start,
ob_end_flush, ob_endclean, and ob get_contents. These are described in detail in
Chapter 8, complete with examples, but I would like to give an overview here.


When you call the ob_start function, anything you send to the browser is placed into a
buffer. This includes text outside of PHP tags. The Web server will not receive this
content until the ob_end_flush function is called. There are several powerful
applications of these functions. One is to avoid the problem associated with sending
headers. Because all headers are sent at once, before any content, you have to take care
when using the header function. This results in a script design where early parts of a
script are declared a "no output" zone, which can be annoying. If you use output
buffering, you can safely add headers to the stack where you wish, and delay sending
content until the last line of your script.


Another application of these functions is in building HTML tables. Imagine creating a
table filled with data from a database. You first print the opening tags for the table. You
execute a query and loop over the results being returned. If everything executes without
error, you print a closing table tag. If an error occurs within the loop, you may have to
abort, and the code that closes the table is never reached. This is bad because of the
behavior of Netscape Navigator: It won't display information inside an unclosed table.
The solution is to turn on output buffering before assembling the table. If assembly
completes successfully, you can flush the buffer. Otherwise you can use ob_end_clean,
which throws away anything in the buffer.


Environment Variables


PHP also makes environment variables available. These are the variables that are created
when you start a new shell. Some are the standard variables like PATH. Others are
variables defined by the CGI. Examples are REMOTE_ADDR and HTTP_USER_AGENT. These
are turned into PHP variables for your convenience. Listing 7.2 tells you which browser
someone is using to surf your page.


Similar to environment variables are the variables the PHP itself creates for you. The first
is GLOBALS, which is an associative array of every variable available to the script.

Free download pdf