Is it safe and feasible to minify PHP files?

Asked

Viewed 2,099 times

11

I was left with this question after reading an answer of this question on HTML. I have only minified with PHP, only with HTML, with PHP and HTML... and I didn’t notice any problems...

Can it become a problem on the server, or in some other way? Minified HTML rendering will always work normal, even with older browsers?

My impression was that it gets much faster, but I’m not sure if it’s always worth it. Is it really worth minifying PHP files? There are a number of lines that make more sense?

UPDATE: I’ll give you two examples of situations:

The first is only with PHP, let’s say a "long" script (for me hehe, about 1,000 lines) that makes calculations with dates and values, where there are only statements of variables, functions, etc., and never use HTML. Is it feasible to minify this file? The gain is worth it?

The second file is where this PHP above is embedded in HTML, template, and it looks something like this:

<html>
<head></head>
<body>
<!-- DIV DO RESULTADO DA SOMA DE N + S -->
<div> 
    <label><?php // resultado da soma de n + s 
echo "Resultado<b>" . $variavelDoScript . "</b>" ?></label> 
</div>
</body>
</html>

Here are small files of a 300 lines this way, always embedding PHP through <?php ?> that mount a page through includes that can make the file with some 3 or 4 thousand lines (including 10 of them for example)... In this case is it worth minifying all these files? Or just remove the comments is enough?

3 answers

6

What you should minify is the content sent to the user’s browser, HTML, Javascript and CSS. Minifying your PHP code does not bring advantages, just complicates your development process.

If your HTML is in the middle of PHP, say with blocks ?> HTML|JS|CSS <?php or divided into echo string it would even make sense, however the ideal is for you to have the "thick" content of that content in separate files that you can minify and upload, in case JS and CSS is simple, keep them in files. js and . css as much as you can trying to meet separation of concepts. For HTML it is a little more complicated because you want to generate it dynamically (after all that is what you are using PHP) however there are alternatives well accepted as the use of templates type languages mustache, Twig and Smarty, which are in separate files that can be minified part of your php code.

Also keep in mind that minifying may be unnecessary in your context, although saving a few Kbs of the user’s band is always a good thing maybe you’re doing an internal project for an environment, not something accessible to the world at www, in this case so that you worry about an increase of a few milliseconds in the page loading being that it will complicate your life.

  • So I added an update with two different situations, if you can take a look... +1

  • @I won’t put it in my answer because I believe it already answers your question, even with the edition you made. But in direct response to your update: your long script that is pure PHP should not be minified because it does not bring any advantage, PHP is not sent to the client and minifying PHP does not speed up your processing. Already your second file looks like a simplified version of the template systems I commented, so yes, this is the file that you should minify because it is where the content that will arrive in the user’s browser lies.

6


If the minification is well done, usually by a reliable (reputable) software or script then there will be no problems with the HTML interpreter even in older browsers, the only thing you maybe you should avoid doing all html inline, thus:

<html><heade></head><body></body></html>

Because even if you hardly come to read the source in production still there may be some need in the future to analyze mainly on pages whose content is dynamic, but if it has a good organization of the development environment, it can rather compress inline

Already the PHP part (codes between <?php and ?>) it is totally unnecessary to minify, because this hardly accelerates at all the delivery of the page and nor the processing of the interpreter, on the contrary you may have several headaches, for example there are many people who write if without {...} when it only has a line after (the problem may occur only in some versions of PHP), even if the PHP script has a million lines of PHP only minifique will not speed up its processing, maybe even hinder the PHP interpreter.

However, there is a native PHP function called php_strip_whitespace that removes blank spaces and line breaks, as quoted by @Wallacemaxters, it can be used like this:

<?php
echo php_strip_whitespace('outro-arquivo.php');

But remember you should not use running, will not bring any benefit, the interesting is to use in systems template creating page caches for example.

If you have html+php, like this:

<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>
    <div>
    ...300 linhas de HTML
    </div>
    <div>
        <?php
        //1000 linhas de PHP
        ?>
    </div>
    <div>
        <?php
        //1000 linhas de PHP
        ?>
    </div>
    <div>
        <?php
        //1000 linhas de PHP
        ?>
    </div>
    <div>
    ...1000 linhas de HTML
    </div>
</body>
</html>

It only pays to compress HTML, PHP doesn’t pay as I mentioned, what you can try to make it easier is to use frameworks that support Views, such as Laravel and Cakephp, so you will separate HTML within Views and most of the logic will be in Controller and Model, or you can also use include to compressed HTML parts like this:

<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<div>
<?php include 'arquivo-com-300-linhas-de-html-comprimidas-em-uma.php'; ?>
</div>
<div>
<?php
//1000 linhas de PHP
?>
</div>
<div>
<?php
//1000 linhas de PHP
?>
</div>
<div>
<?php
//1000 linhas de PHP
?>
</div>
<div>
<?php include 'arquivo-com-1000-linhas-de-html-comprimidas-em-uma.php'; ?>
</div>
</body>
</html>

As for PHP comments, it doesn’t pay to remove them because it won’t make much difference in performance, whereas HTML comments if they are too long or too long are better to remove.

Tools to compress HTML:

You might also be interested in minifying files like CSS and JS:

Speeding up the PHP

One detail that is worth mentioning is that PHP is an interpreted language, I mean to each request the PHP files will be reprocessed before they are executed, the language itself does not have natively JIT (Just in time), however from PHP5.5 we own the Opcache (it is necessary to enable), in older versions it is necessary to install manually or via PEAR or even compile manually (most likely on Linux servers).

Extensions like Opcache and Xcache greatly improve the performance of sites in PHP, as it will not be necessary to interpret the scripts for each request, more details in:

  • 2

    If you can add the reference, in php we have a function called php_strip_whitespace, which returns a compressed php code. You need to pass the script name as argument :D

  • @Interesting wallacemaxters to use in cached templates right :) hehehehe. I will add.

  • So I added an update with two different situations, if you can take a look... +1

  • 1

    It’s true, I’m implementing it today on Legendary\View :)

  • 1

    @Guilhermenascimento I’m not sure, but I think php_strip_whitespace removes comments as well ;)

  • So, in the case does not include so many lines of php, at the beginning of the file has the include of the script, only PHP part, that already understood that it is not worth minificar right... but in these other files is more 300 lines of HTML each with some PHP variables embedded in the middle, at most some function call... so it’s much more HTML and just some PHP. But when it mounts, it can get a very large file, so in this case it is no problem to have the calls of PHP variables in the middle?

  • 1

    @gustavox when you only use something like <div><?php echo 'oi'; ?></div> and not having a complex logic so you can minify everything, including php ;)

Show 2 more comments

3

You won’t have any problems, spacing, tabulations are more visual than really necessary.

However minification is only recommended if you have something that does this for you, I mean, that you still have your source code indented correctly. Otherwise it will be a strain to maintain.

Actually it can get faster loading as you remove good bytes of white space, in mobile browsers (which are usually slower) the difference can be quite remarkable.

I don’t know any Minify tools in PHP for css, js, etc.

But if the form of Minify is direct in the code forget! Earnings gain is so minimal (and also for SEO) that it is not worth Minify PHP.

NOTE: Actually it is impossible to minify PHP, what you do is Minify the html generated by HTML, since PHP is interpreted by Server and sent html to client.

  • So I added an update with two different situations, if you can take a look... +1

Browser other questions tagged

You are not signed in. Login or sign up in order to post.