W3docs

php_strip_whitespace()

In this article, we will focus on the PHP php_strip_whitespace() function. We will provide you with an overview of the function, how it works, and examples of

The PHP php_strip_whitespace() function reads a PHP source file and returns its contents with all comments and unnecessary whitespace removed — exactly the output you would get from running the file through php -w on the command line. This page covers the syntax, what the function does (and does not) strip, a complete runnable example, performance trade-offs, and how it compares with the string-trimming functions people often confuse it with.

Syntax

php_strip_whitespace(string $filename): string
PartMeaning
$filenamePath to the PHP file to read and strip.
Return valueA string containing the file's source with comments and redundant whitespace removed. Returns an empty string on failure (for example, if the file does not exist).

The function does not execute the file — it only tokenizes its source. It also does not modify the original file on disk; it returns the stripped source as a new string, leaving you to decide what to do with it.

What gets stripped

php_strip_whitespace() uses PHP's own tokenizer, so it strips with full awareness of PHP syntax — it never breaks your code by stripping whitespace that matters. Specifically, it removes:

  • All comment styles: // line, # line, and /* block */ (including /** docblocks */).
  • Redundant whitespace between tokens, collapsing runs of spaces, tabs, and newlines.

It is careful to preserve whitespace inside string literals and any content outside <?php … ?> tags (plain HTML), because removing that would change the program's output.

How to use php_strip_whitespace()

Pass the path of the file you want to strip. The example below writes a small file, then strips it so you can see the difference:

<?php
// Create a sample file with comments and generous spacing.
$source = <<<'PHP'
<?php

// Greet the visitor.
function greet(string $name): string
{
    /* Build the message */
    return "Hello,   $name!";   // spaces inside the string are kept
}

echo greet("World");
PHP;

file_put_contents('sample.php', $source);

// Strip comments and whitespace.
$stripped = php_strip_whitespace('sample.php');

echo $stripped;

The comments and extra blank lines are gone, while the spacing inside the "Hello, $name!" string is left untouched, because changing it would change the program's behavior.

If the file cannot be read, the function returns an empty string rather than raising a fatal error:

<?php
$result = php_strip_whitespace('does-not-exist.php');

var_dump($result === ''); // bool(true)

When to use it

  • Shipping a production build. Stripping comments and whitespace shrinks source files and slightly reduces parse time on cold loads (with an opcode cache like OPcache enabled, that win is mostly absorbed after the first request).
  • Obfuscating distributed code. Removing comments and docblocks makes shipped source less self-documenting — a light, not secure, form of obfuscation.
  • Inspecting tokenized output. It is a quick way to see a file's source without its comments.

Performance considerations

Use php_strip_whitespace() only on production copies of your files, never on your working source. During development, comments and whitespace make code readable, debuggable, and maintainable; stripping them in place would throw that away. The standard approach is to keep commented source under version control and generate stripped copies as part of a build/deploy step.

Note that with OPcache (bundled and enabled by default in modern PHP), compiled bytecode is cached after the first request, so the parse-time savings from stripping are marginal. The size reduction is the more reliable benefit.

php_strip_whitespace() vs. trim()

These are unrelated despite the similar-sounding names — a common source of confusion:

FunctionOperates onRemoves
php_strip_whitespace()a PHP source filecomments + redundant whitespace from source code
trim()a string valuewhitespace from the start and end of a string
ltrim() / rtrim()a string valuewhitespace from one end only

If your goal is to clean up a string (such as form input), reach for trim(), not php_strip_whitespace().

  • file_get_contents() — read a file's raw contents without stripping anything.
  • readfile() — read a file and write it straight to the output buffer.
  • highlight_string() — the opposite goal: render PHP source with syntax highlighting.
  • PHP comments — the comment syntaxes this function removes.

Conclusion

php_strip_whitespace() returns a comment- and whitespace-free copy of a PHP source file by running it through PHP's tokenizer, so it shrinks files safely without breaking valid code. Reserve it for production builds, remember it works on files (not strings), and don't confuse it with trim().

Practice

Practice
What is the functionality of the PHP 'trim()' function as employed in programs?
What is the functionality of the PHP 'trim()' function as employed in programs?
Was this page helpful?