php_strip_whitespace

Returns the source of a PHP file with all comments and unnecessary whitespace removed.

Signature
php_strip_whitespace(string $filename): string
  • Added in PHP 5.0

Parameters

ParameterTypeDescription
$filenamestringPath to the PHP file to read. Relative paths are resolved against the include path as well as the working directory.

Return value

stringThe stripped source code, or an empty string if the file could not be read or contains no code.

Errors

Does not throw. An unreadable file raises a warning and yields an empty string.

php_strip_whitespace() reads a PHP file through the tokenizer and gives you back the same code with comments gone and runs of whitespace collapsed. It is the library equivalent of running php -w file.php.

When to reach for it

  • Measuring how much of a file is real code versus comments and blank lines.
  • Shrinking generated or bundled PHP before shipping it, where the comments carry no value.
  • Producing a normalised form of a file for a quick structural comparison.

What trips people up

It takes a filename, not source code. There is no string variant; write the code to a temporary file first, or tokenise it yourself with token_get_all().

Failure is quiet. A missing or unreadable file gives you an empty string and a warning, which looks identical to a file that legitimately contains nothing, so check the path with is_readable() before trusting the result. Note also that it strips all comments, including docblocks that annotations, attributes-era tooling or reflection-based frameworks depend on, and including any licence header you may be required to keep. It is not a general minifier either: string literals and heredocs are preserved byte for byte, so the savings come only from comments and layout.

Example

Try it yourself
Loading editor…
Press Run to execute the code.

When to use it

  • Reporting the comment-to-code ratio of files in a codebase audit.
  • Compacting generated PHP config or container files before deployment.
  • Producing a whitespace-insensitive version of two files to compare their real content.

Frequently asked questions

Can php_strip_whitespace work on a string instead of a file?

No, it only accepts a filename. To strip a string, write it to a temporary file first or process it yourself with token_get_all().

Does php_strip_whitespace minify PHP?

Only partially. It removes comments and collapses whitespace, but it does not rename variables, shorten code or touch string literals.

Why did it return an empty string?

The file could not be read, or the path was wrong. Check with is_readable() first, because failure and an empty file look identical in the return value.

More examples

Measuring comment weight

The byte counts show how much of the file was comments and blank lines; the percentage is a rough comment-and-layout overhead figure for that file.

Example · php
<?php
$file = 'weighty.php';
$source = "<?php\n// header comment\n/** docblock */\n\n\n\$x = 1;\n";

file_put_contents($file, $source);

$stripped = php_strip_whitespace($file);
printf("original: %d bytes\n", strlen($source));
printf("stripped: %d bytes\n", strlen($stripped));
printf("saved:    %d%%\n", 100 - (int) (strlen($stripped) / strlen($source) * 100));

unlink($file);

Missing files fail silently

The guard prints instead of returning a confusing empty string, and the var_dump shows that a real, readable file produces non-empty output, so an empty result really does mean failure.

Example · php
<?php
$path = 'does_not_exist.php';

if (!is_readable($path)) {
    echo "guarded: file is not readable\n";
} else {
    echo php_strip_whitespace($path);
}

var_dump(php_strip_whitespace(__FILE__) === '');

Discussion

  • Be the first to comment on this lesson.