Cathony Cathony - 4 months ago 6x
HTML Question

Will minifying the HTML portion of a PHP file have an effect on performance and bandwidth?

Say I had, for example:

<p>Here's a list of some stuff</p>
foreach ($examples as $example) {
echo "<li>" . $example . "</li>";

I know minifying such a file is possible, but my question has more to do with whether or not it is worth doing.

Would minifying the HTML portion of such a file make any performance difference? And if it would, would it still do so if it were used through
rather than a browser asking for

And would those performance differences only affect the processing time of the interpreter, or would they also have a difference on the amount of data ultimately sent to the browser?


Yes, it has an effect on the bandwidth. Usually, minifying/uglifying is done with JavaScript code and CSS styles, because it has a large effect there. There is large overhead, if e.g. a library is not minified/uglified. With HTML, it obviously has the same effect to the bandwidth, but in a much lower range. Your HTML snippet for example can be minified and 119 Bytes can be saved (ASCII). This is 39%, but with a very low absolute value of 119 Bytes.

Therefore, I would suggest to only minify your markup if you have an unusual large portion of HTML or if you are using HTML templates. With HTML templates, it is very easy to minify it but if you are outputting your HTML markup with PHP's echo line by line, you would have to do the minification manually.

Make also sure to also enable gzip. With gzip compression you save a lot more, than by minifying the HTML. You can enable it e.g. with that command (before the first outputting line).

<?php ob_start("ob_gzhandler"); ?>