How to Compress PHP Files With the PHP Function Gzdeflate
If you use a web hosting service that supports gzip compression (most do) then you probably already know how to compress your php files with mod_gzip. However, if you haven't tried it or need to compress your files with an alternative method here is a quick and easy way to do it.
This snippet of code works by buffering all output from your script until the end and then running it through a function that gzips the output before it is sent to your browser. It is quick and easy but does require that you send a 'Content-Encoding: gzip' header to your browser so they know the payload is compressed. It also only works if your web server supports the gzip and deflate compression algorithyms.
You'll want to check the PHP manual page for this function for further details. In short it compresses a raw string using the DEFLATE data format, for details on the format see the "DEFLATE Compressed Data Format Specification version 1.3" (RFC 1951). The level of compression, can be from 0 for no compression up to 9 for maximum compression. The original compressed string or FALSE on error.
You can also use this snippet to obfuscate your PHP code so that it cannot be read by malicious hackers. This is done by encoding the string with str_rot13() and base64_encode() then using gzdeflate() to recompress it back to readable form. This can be useful for securing sites that host sensitive information but it may not be a good idea for sites where security isn't the main concern.