Content Compression Using PHP

In the old days, programmers had to define which compression method that was to be used to send data from the server to the client; usually gzip or deflate. Now, with languages like PHP that do more, programmers do not have to worry, since PHP will automatically choose the correct type of compression that is to be used. Compressing data results in a less amount of data being sent over the network and the main gain being speed. Thus one of the key benefits of using content compression is that bandwidth usage is decreased. Network latency can also be reduced due to a fewer number of packets being exchanged over the network, thus resulting in your application appearing faster.

In PHP there are two ways that you can use content compression. One is to use zlib.output_compression. However to do this your PHP will have to be built with the zlib extension. You can also change the level of compression and set it to minimal compression or maximum. The other method is to use ob_start. Using the Output Buffer system allows you to use content compression even if your ISP does not give you access to your php.ini file, which is required to activate the zlib extension. To use Output Buffering add this code to the top of the page that you want compressed. This needs to be added to top and called before any other scripts are executed. The code is ob_start(‘ob_gzhandler’); By using this script PHP will compress the content and select the type of compression to use and then transmit the data between the client and server.