PHP Minifier
Shrink PHP source by removing comments and the whitespace PHP does not need. The minifier reads the code token by token, so the contents of strings, heredocs and the HTML around your PHP blocks are never altered.
- Runs in your browser
- No sign-up
- Free to use
How to use PHP Minifier
- Paste PHP code or open a .php file.
- Choose full minification or comment removal only, and whether to keep licence comments.
- Select Minify PHP.
- Copy or download the result, and run it once to confirm it behaves the same.
PHP Minifier features
Token-aware
Strings, heredocs, nowdocs and backtick commands are recognised and copied byte for byte.
Inline HTML preserved
Text outside <?php … ?> blocks is not touched, so templates keep their output.
Safe spacing
A space is kept wherever removing it would merge two tokens, such as “+ +” or “1 . 2”.
Comments-only mode
Strip comments and docblocks while keeping the formatting readable.
Licence comments
Comments beginning with /*! or containing @license can be kept.
Private
Runs in the browser; source code is not uploaded.
When to use PHP Minifier
- Removing comments before handing code to a client or publishing a snippet.
- Reducing the size of a single-file script or a plugin download.
- Packing a small PHP file into a configuration value or an installer.
- Seeing how much of a file is comments.
PHP Minifier FAQ
Does minifying PHP make a site faster?
Hardly. PHP compiles source to bytecode, and with OPcache, which is enabled on practically every production server, the compiled form is cached. Comments and whitespace are gone after compilation either way. Minification saves disk space and transfer size, not execution time.
Then why minify PHP at all?
To remove comments that should not be distributed, to make a distributable file smaller, or to fit code into a constrained place. It also makes casual reading less convenient, though it is no protection: a formatter restores the layout in a second.
Is the minified code guaranteed to behave the same?
Whitespace and comments have no meaning in PHP outside strings and inline HTML, and those are preserved exactly, so behaviour is unchanged. One exception worth knowing: code that reads its own doc comments through reflection, as some annotation-based frameworks do, depends on comments and must not have them removed.
What happens to heredocs?
Heredoc and nowdoc blocks are copied unchanged from the opening <<< line to the closing marker, including their indentation, and the statement after them stays on its own line for compatibility with older PHP versions.
Are variable names shortened?
No. Renaming is unsafe in PHP, because variables can be accessed by name through features such as variable variables, compact(), extract() and templates. Only comments and whitespace are removed.
Does PHP have a built-in way to do this?
Yes: php -w file.php on the command line, or the function php_strip_whitespace(), produce a similar result. This tool does the same job without a PHP installation and adds the comments-only mode.
What can safely be removed from PHP source
To the PHP engine, a script is a stream of tokens: keywords, names, operators, literals. Whitespace merely separates tokens, and comments are discarded before compilation. In principle all of it can go, as long as two tokens that need a separator still have one. “return $x” may become “return$x”, but “new Foo” may not become “newFoo”. The minifier applies this rule token by token, keeping a single space only where two neighbours would otherwise fuse.
The difficulty lies in knowing what is code. The characters // inside a string are not a comment, and several spaces inside a heredoc are data that must reach the output. A minifier based on search and replace gets these cases wrong sooner or later. This one scans the source the way PHP's own tokenizer does. It recognises string literals with their escape rules, interpolated expressions in double-quoted strings, heredoc and nowdoc blocks, and the switch between PHP and inline HTML, and it copies all of those unchanged.
It is worth being clear about what minifying PHP achieves. Unlike JavaScript, PHP source is not sent to visitors. It stays on the server, where it is compiled once and cached. The size of the source therefore has no measurable effect on response time. The real uses are distribution and tidiness: a smaller archive, a file without internal remarks, or a script that has to fit a limit.
Keep the original. Minified code is unpleasant to debug, error messages refer to very long lines, and doc comments that tools and editors rely on are gone. Treat the minified file as a generated artefact, produced from readable source that remains under version control.