0

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?

Cat
  • 60
  • 10
  • 1
    http://stackoverflow.com/questions/4079920/is-there-a-point-to-minifying-php?rq=1 – j08691 Jun 21 '16 at 17:01
  • @j08691 Does the answer to that question apply to the entire PHP file including the html outside the ``, or only to the PHP code itself? – Cat Jun 21 '16 at 17:04
  • other than removing line breaks and some indentation, there's very little about html that can be minified. it's not a programming language, so you can't shrink an `` tag down to `` - now it's italics, not an image anymore. – Marc B Jun 21 '16 at 17:32
  • The PHP interpreter doesn't care about the text outside the `` markers. Minifying it, however, reduces the amount of data sent through the network and the page loads (a little) faster. – axiac Jun 21 '16 at 17:49

2 Answers2

1

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.

Community
  • 1
  • 1
Jonathan
  • 10,936
  • 8
  • 64
  • 79
0

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"); ?>
ssc-hrep3
  • 15,024
  • 7
  • 48
  • 87
  • Just for a bit of clarification, by HTML templates you're referring to a pure or nearly pure-HTML file I `include()` or `require()`, as well as HTML that is not added through `echo`, correct? – Cat Jun 21 '16 at 17:23
  • Yes, with PHP you can use `include()` or `require()`, other languages or frameworks have a built-in template engine. E.g. in Angular.js e.g. you can minify HTML files and pass them to the routing controller which uses it's template engine to render them – ssc-hrep3 Jun 21 '16 at 17:27