Is it safe and feasible to minify PHP files?

10

I was left with this question after reading a HTML question . I have mined only with PHP, only with HTML, with PHP and HTML ... and I did not notice any problems ...

Can it become a problem on the server, or in some other way? Will rendering mined HTML 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 worth to minify PHP files? Is there a number of lines that make the most 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 thousand lines) that calculates dates and values, where only declarations of variables, functions, etc., and never use HTML . Is it possible to minify this file? Is the gain worth it?

The second file is where this PHP above is embedded in the HTML, template, and 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 in this way, always embedding PHP through <?php ?> that set up a page through includes that can make the file with 3 or 4 thousand lines (including 10 of them for example ) ... In this case is it worth minifying all these files? Or just taking the comments off is enough?

    
asked by anonymous 18.02.2016 / 16:47

3 answers

6

If the minification is well done, usually by a trusted (reputable) software or script then there will be no problems with the HTML interpreter even in older browsers, the only thing you might should avoid is to make the entire html inline , like this:

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

Even though you can hardly read the source in production, there may still be a need in the future to analyze mainly pages whose content is dynamic, but if you have a good organization of the development environment, you can compress < in> inline

The PHP part (codes between <?php and ?> ) is totally unnecessary to minify, as does not accelerate almost anything page delivery and interpreter processing, on the contrary you can have multiple headaches, for example there are many people who write if without {...} when it only has one line later (the problem may only occur in some versions of PHP), even if the PHP script has a million PHP-only minify lines will not speed up processing it, it might even bother the PHP interpreter.

However, there is a native PHP function that calls php_strip_whitespace that removes spaces blank 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, it will not bring any benefit, the interesting thing is to use template systems that create 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>
Just pays to compress the HTML, PHP does not compensate as I mentioned, what you can try to make easier is to use frameworks that support Views, such as Laravel and Cakephp, so you will separate the HTML inside the Views and most of logic will stay in Controller and Model, or you can also use include for 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 does not pay to remove them since it will not make much difference in performance, since the HTML ones if they are too long or too long is better to remove.

Tools to compress HTML:

You may also want to minify files like CSS and JS:

Accelerating PHP

A detail worth mentioning is that PHP is an interpreted language, I mean at 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 have Opcache (you must enable it), in older versions you must install manually or via PEAR or even compile manually (most likely on Linux servers).

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

18.02.2016 / 17:19
6

What you should minify is content submitted to the user's browser, HTML, Javascript, and CSS. Minimizing your PHP code does not bring advantages, it only complicates your development process.

If your HTML is in the middle of PHP, let's say with ?> HTML|JS|CSS <?php or fractioned blocks in echo string , it would even make sense, but ideally you have the "bulk" of that content in separate files that you can minify and send , in the case of JS and CSS is simple, keep them in .js and .css files as long as you can trying to meet separation of concepts . Now for HTML it's a bit more complicated because you want to generate it dynamically (after all that's why you're using PHP), however there are well-accepted alternatives like using template languages like mustache , twig and smarty , which are contained in separate files that can be minified to part of your php code.

Also keep in mind that minify may be unnecessary in its context, although saving user bandwidth KBs is always a good thing, maybe you are doing an internal project for an environment, not something accessible to the world on the www, in this case so you have to worry about adding a few milliseconds to the page loading as this will complicate your life.

    
18.02.2016 / 17:19
3

You will not have any problems, spacing, tabs are more visual than really necessary.

But minification is only recommended if you have something that does this for you , I mean, you still have your source code properly indented. Well, it will not be enough to maintain it.

You can actually get faster loading because you remove good bytes of white space, in mobile navigations (which are usually slower) the difference can be quite remarkable.

I do not know of any tool that does minify in PHP Only for css, js, etc.

But if the form of minify goes straight into the code, forget it! The Gain of disengagement is so minimal (and also for SEO) that it is not worth minify PHP.

  

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

    
18.02.2016 / 16:57