05.05.2013 Views

Programming PHP

Programming PHP

Programming PHP

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

}<br />

ob_start('rewrite');<br />

?><br />

Visit our site now!<br />

Visit our site now!<br />

Compressing Output<br />

Recent browsers support compressing the text of web pages; the server sends compressed<br />

text and the browser decompresses it. To automatically compress your web<br />

page, wrap it like this:<br />

<br />

The built-in ob_gzhandler( ) function is designed to be used as a callback with ob_<br />

start( ). It compresses the buffered page according to the Accept-Encoding header<br />

sent by the browser. Possible compression techniques are gzip, deflate, or none.<br />

It rarely makes sense to compress short pages, as the time for compression and<br />

decompression exceeds the time it would take to simply send the uncompressed text.<br />

It does make sense to compress large (greater than 5 KB) web pages, though.<br />

Instead of adding the ob_start( ) call to the top of every page, you can set the<br />

output_handler option in your php.ini file to a callback to be made on every page. For<br />

compression, this is ob_gzhandler.<br />

Error Handling<br />

Error handling is an important part of any real-world application. <strong>PHP</strong> provides a<br />

number of mechanisms that you can use to handle errors, both during the development<br />

process and once your application is in a production environment.<br />

Error Reporting<br />

Normally, when an error occurs in a <strong>PHP</strong> script, the error message is inserted into<br />

the script’s output. If the error is fatal, the script execution stops.<br />

There are three levels of conditions: notices, warnings, and errors. A notice is a condition<br />

encountered while executing a script that could be an error but could also be<br />

encountered during normal execution (e.g., trying to access a variable that has not<br />

been set). A warning indicates a nonfatal error condition; typically, warnings are displayed<br />

when calling a function with invalid arguments. Scripts will continue executing<br />

after issuing a warning. An error indicates a fatal condition from which the script<br />

cannot recover. A parse error is a specific kind of error that occurs when a script is<br />

syntactically incorrect. All errors except parse errors are runtime errors.<br />

This is the Title of the Book, eMatter Edition<br />

Copyright © 2002 O’Reilly & Associates, Inc. All rights reserved.<br />

Error Handling | 303

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!