Cat
Cat

Reputation: 50

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

Say I had, for example:

<body>
    <h1>Title</h1>
    <article>
        <h2>Article</h2>
        <p>Here's a list of some stuff</p>
        <ul>
        <?php
            foreach ($examples as $example) {
                echo "<li>" . $example . "</li>";
            }
        ?>
        </ul>
    </article>
</body>

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 require('file.php') rather than a browser asking for file.php?

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?

Upvotes: 0

Views: 422

Answers (2)

ssc-hrep3
ssc-hrep3

Reputation: 16089

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"); ?>

Upvotes: 0

Jonathan
Jonathan

Reputation: 11494

If your HTML is inside the PHP script, and you're minifying the PHP script, you might assume that the HTML is also being minified along with it. But that depends on how your minifier works.

See this question on minifying HTML. It would seem the answer is no, it does not have an affect on performance worth your time.

Upvotes: 1

Related Questions