Minify HTML in PHP

In this article, I am going to discuss Minify HTML in PHP. Why and how you need to Minify HTML in your web page. How it works and what is the facility of web page minifying. Let's see the answer of above questions.

Why You Minify HTML?

A website speed depends on page structure. If you don't compress website pages yet your website will take a long time to load. If you Minify it will be faster to load and it is very effective to increase user experience. So, For Increase Website Page Speed you should Minify HTML of your website. Even your website's search engine optimization score depends on website speed and search engines always recommends to minify HTML and make a super fast website. We have already known the importance of minifying web pages. Now the issue is how we minify web pages and I am going to describe you about it.

How To Minify HTML?

If you have a static website or If you do not use PHP in your website then you can minify your web pages manually using online HTML compression tool and IF you have a dynamic website or If you use PHP in your website then you can minify your web pages using PHP.
A PHP function helps you to minify web pages automatically. So we need to see that how PHP do that and how to use it.

How To Minify HTML In PHP?

In that way, we are going to see how we can minify web page in PHP. We need to create a PHP function and put it top of the page to compress your web page. Let's see this function,

$GZipEncodingEnable = true;
function GZipAccepts()
$accept = str_replace(" ","",strtolower($_SERVER['HTTP_ACCEPT_ENCODING']));
$accept = explode(",",$accept);
return in_array("gzip",$accept);
function MinifyHTML($HtmlOutput)
return preg_replace("/\s+/"," ",$HtmlOutput);
function CompressPage($HtmlOutput)
global $GZipEncodingEnable;
$HtmlOutput = MinifyHTML($HtmlOutput);
if(!GZipAccepts() || headers_sent() || !$GZipEncodingEnable) return $HtmlOutput;
header("Content-Encoding: gzip");
return gzencode($HtmlOutput);
If you define this code top of your web page then this function will Minfy HTML of your web page.
HTML Minfy Code
You can add this code in your website's every page by using PHP include();  function. Create a PHP file name as minify.html.php and write this code into this file and include this file every page of your website.

How Minify HTML works?

This code compress your web page and decrease your page size. So page will be faster. You should minify javascript and minify CSS to get more effective result and get more fast page load.

Facilities of HTML minify

Websites gets some extra facilities when a webmaster minify their websites, now we are going to see what type of facility gets when a website minify their web page correctly,
Faster Web Page: When you minify web page then it will decrace your web page size and it will be faster to load.
Improved User Experience: When your website load correctly then you get maximum possibility to back this user again. This user will return when he need to do anything or any need.
Search Engine Priority: Every search engine likes faster website because they care about user experence, If your website's load time is fast then you will get more priority from search engine and your search click will accelerate. You'll reach top of search engines and you'll get a lot of users from Search engine.

If you do not Minify HTML of your web page yet dot it now as soon as possible. You should care about others effective side which can increase your website more fast more search engine (SEO) friendly. You shouldn't miss 5 Tips To Increase Website Page Speed. It will be more beneficial when you Minify HTML every page of your website using PHP. I hope this article will help you to Minify HTML in PHP. If you have any question or comment about it please don't hesitate to do that, please comment your question in comment section.

Download Source Code
Unlock source code with a social share.

Did you like this article? it will be appreciated if you share a coffee or burger with the author

Sent $5 to the author
Sent $10 to the author

Need Assistance?

I'm Sajjad Hossain, working on web application development since 2012. Do you need assistance on your project? or are you stuck with problems? I am available to help you.
If you want to contact with me ping me at -


We use cookies on our website. To find out more about how and why they are used or opt-out, please read our Cookie Policy. By choosing "I Accept", you consent to our use of cookies. Cookie Policy