php8.5
Home/ Manual/ misc / functions/ php_strip_whitespace

php_strip_whitespace

PHP function Edit on GitHub ✎

(PHP 5, PHP 7, PHP 8)

Return source with stripped comments and whitespace

Description

php_strip_whitespace(string $filename): string

Returns the PHP source code in filename with PHP comments and whitespace removed. This may be useful for determining the amount of actual code in your scripts compared with the amount of comments. This is similar to using php -w from the commandline.

Parameters

filename

Path to the PHP file.

Return Values

The stripped source code will be returned on success, or an empty string on failure.

Note

This function respects the value of the short_open_tag ini directive.

Examples

php_strip_whitespace() example

php
<?php
// PHP comment here

/*
 * Another PHP comment
 */

echo        php_strip_whitespace(__FILE__);
// Newlines are considered whitespace, and are removed too:
do_nothing();
?>

The above example will output:

output
<?php
 echo php_strip_whitespace(__FILE__); do_nothing(); ?>

Notice the PHP comments are gone, as are the whitespace and newline after the first echo statement.

Source: reference/misc/functions/php-strip-whitespace.xml · from the official PHP manual (php/doc-en)