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:
remove comments from html or php?
– Guilherme Nascimento
From both @Guilhermenascimento.
– gustavox
I edited the answer ;)
– Guilherme Nascimento