When to compress a string?

When would you ever want to compress a string with a function like gzcompress() ? Or any other of PHP’s compression methods?

Other than to conserve database space, in what situation is it desirable to compress a string?


Never contemplate it anymore Don’t know why you would even think of it unless you have run into a place where you have some HUGE (as in mb size) strings.

Twenty years ago, this discussion had a place. Nowadays though the price of storage is not much of a factor so why worry about space?

This is point of view is narrow - of course, compression makes a lot of sense in certain situations and is used nowadays. Some use cases:

  1. Sending data over network is usually much faster when compressed. Browsers support gzip compression so we can send html, css and js files in a compressed format. For example, you can make a php script that will concatenate all your css or js files, compress them and send in one go to the browser. The speed gains are considerable, especially for people on slow connections. There are also other types of files like logs, etc. that you can compress for download and make the process faster.

  2. For archiving purposes it makes a lot of sense to compress files because they are accessed very rarely and sometimes there can be a lot of them and compression can save lots of space.

While hard disks are cheap hosting space is not that cheap. If you own your server then you can buy multi-terrabyte disks easily but if you have a hosting plan you are restricted to how much you can use. Then compression makes sense.