PHP Minifier
Strip comments and collapse whitespace from PHP to shrink file size.
What is PHP Minifier?
PHP Minifier strips comments and unnecessary whitespace from a PHP source file to shrink it for production, entirely in your browser. It removes `//`, `#`, and block comments, and collapses every run of spaces, tabs, and newlines outside of quoted strings and heredoc/nowdoc blocks, while leaving the contents of single- and double-quoted strings (with escapes), heredoc/nowdoc bodies, and the `<?php` / `?>` tags completely untouched. A space between two word-like tokens (an identifier, variable, number, or quoted string) is always preserved so two tokens never get accidentally merged into a different one — so `echo "x"` stays `echo "x"` and `foo /* c */ bar` stays `foo bar` instead of collapsing into the broken identifier `foobar`. It shows you exactly how many bytes and what percentage you saved before you ship the result.
How to Use PHP Minifier
- Paste your PHP into the input box.
- The minified result appears instantly below, along with the bytes and percentage saved.
- Copy the minified PHP with the copy button.
Examples
Comments stripped and tags preserved
Input: <?php // greet the user echo "Hello"; # trailing comment ?>
Output: <?php echo "Hello";?>
Whitespace collapsed around operators
Input: <?php function add( $a , $b ) { return $a + $b ; } ?>
Output: <?php function add($a,$b){return $a+$b;}?>
Strings containing // and # are left verbatim
Input: <?php echo "https://example.com #tag"; ?>
Output: <?php echo "https://example.com #tag";?>
Common Mistakes
- Minifying PHP as your only working copy — always keep a formatted, commented source file and minify a copy of it, not the other way around.
- Expecting dead-code elimination, variable renaming, or opcode optimization — this tool only removes whitespace and comments, it doesn't rewrite what the code does.
- Using it on PHP templates that interleave `?>` ... HTML ... `<?php` and expecting the literal HTML between the tags to be preserved byte-for-byte — it targets pure PHP code blocks; minify template files carefully and review the diff.
- Forgetting that minified PHP is much harder to debug in production — map back to source, or keep the beautified version handy (see the PHP Beautifier tool).
Why Use This Tool
- Processes entirely client-side — your PHP never leaves your browser.
- Quoted strings and heredoc/nowdoc bodies are protected from whitespace collapsing and comment stripping.
- Keeps a separating space where two tokens would otherwise merge, so the output always parses like the input.
- Shows exactly how many bytes and what percentage were saved.