PHP Minifier
Strip comments and whitespace without breaking anything.
About the PHP Minifier
What is a PHP minifier?
A PHP minifier strips out everything the PHP engine never reads. Comments, docblocks, indentation, blank lines and the spaces between operators all go, and what comes back is the same program in fewer bytes. Unlike minifying JavaScript or CSS, this is not about download size, since visitors never receive your PHP. It is about smaller files to ship and read from disk, and about not handing your internal comments to anyone who ends up with a copy of the source.
The risk with minifying PHP is that a careless tool will reach into places it should not. Strings, heredocs and nowdocs carry your actual output, and everything outside the PHP tags is HTML that goes straight to the browser, so touching any of it changes what your program does. This one is built on a real PHP tokenizer rather than pattern matching, so those regions are copied through untouched, and every result is compared token by token against the original before it is shown to you.
How to Use This Tool
- Add your PHP. Paste it into the left editor, click Open file, or hit Sample to see it work.
- Choose what to keep. Turn on Keep docblocks if a framework reads your annotations, and Collapse inline HTML only if you also want the markup squeezed.
- Check the badge. It confirms the output was token-verified against your input and shows how many bytes you saved.
- Take the result. Copy it to your clipboard or download it as a
.phpfile.
Example
With the default settings, comments and docblocks go and the spacing collapses. This input will be converted to a single compact line:
<?php
/**
* Apply the standard tax rate.
*/
function withTax(float $price): float
{
// Standard rate
return $price * 1.2;
}
<?php function withTax(float$price):float{return$price*1.2;}
That is 55 percent smaller with identical behavior. The space in float $price goes because $ cannot continue an identifier, so the two stay separate tokens either way. Elsewhere a space has to survive: new Foo stays spaced or it becomes one name, and 1 - -1 minifies to 1- -1 because 1--1 would read as a decrement. Knowing which spaces are load bearing is what a tokenizer buys you over a search and replace.
Common Use Cases
- Shipping a plugin or theme: Send a smaller bundle and leave your working notes behind.
- Stripping comments before handover: Remove internal TODOs and reviewer notes from code going to a client.
- Trimming a large include: Cut the bytes PHP reads from disk on every request for a big config or data file.
- Cleaning up generated code: Compress output from a scaffolding tool that emits heavy docblocks.
- Fitting a snippet somewhere small: Squeeze a working script into a field or a form that has a character limit.
Need it readable again? The PHP Formatter expands it back out, and the PHP Validator checks it parses first. Or browse all our free developer tools.
Frequently Asked Questions
Will minifying break my code?
It should not, and the tool checks rather than assuming. After minifying, it runs the result back through the tokenizer and compares it to the original token by token. If a single token differs, the output is withheld and you get a warning instead of broken code. We also ran the engine over 1,198 real PHP files, including the Stripe and PHPMailer libraries, and compared every result against PHP's own tokenizer. All 1,198 came back identical and still parsed.
Does it touch my strings or HTML?
No. Single quoted strings, double quoted strings, heredocs, nowdocs and backticks are copied through byte for byte, because their contents are your program's output. The same goes for anything outside <?php and ?>, since that is HTML sent straight to the browser and its whitespace can matter inside a <pre> tag. If you do want the markup squeezed too, the Collapse inline HTML switch does that, and it is off by default for exactly that reason.
Why keep docblocks?
Because some code reads them at runtime. Doctrine annotations, older Symfony routing and several dependency injection containers pull configuration out of /** */ blocks, so stripping them silently breaks the application. Regular //, # and /* */ comments are always removed. Turn on Keep docblocks if your framework is one of the ones that reads them, and leave it off otherwise.
Does it rename variables to save more space?
No, and that is deliberate. Renaming variables in PHP is not safe to do from the outside, because $$name, extract(), compact() and variable interpolation inside strings can all reach a variable by its name at runtime. A minifier cannot see those uses reliably, so anything that renames is gambling with your code. This one only removes what the parser throws away, which is why the output is guaranteed to behave the same.
How much smaller will my file get?
Across those 1,198 real files the average saving was about 38 percent, though it depends entirely on how heavily commented the source is. A file thick with docblocks can lose more than half its bytes, while one that is already dense might only give up ten percent. The badge shows the exact before and after for whatever you paste in.
Will this make my website faster?
Barely, and it is worth being honest about that. Visitors never download your PHP, so unlike CSS or JavaScript there is no transfer saving. What you get is slightly less disk reading and parsing per request, which opcode caching already handles well on most servers. Minify PHP to ship smaller files or to strip comments from code you are handing over, not as a page speed fix.
Is my code uploaded anywhere?
No. Minification runs in your browser with a locally served engine, no CDN and no server round trips. Your code never leaves the machine, and the tool keeps working offline once the page has loaded.