The php_strip_whitespace() function is a built-in PHP function used to remove all comments and whitespace characters from a specified PHP file, resulting in a more concise code output.
php_strip_whitespace(string $filename): string
The parameter $filename specifies the PHP file path from which comments and whitespace will be removed. The function returns a string containing the code without comments or whitespace.
Using php_strip_whitespace() effectively reduces the size of PHP files, decreases server load time, and improves website performance and response speed.
Since this function removes all comments, important comment information in your code should not depend on being present after the function is applied.
The function removes all whitespace characters, including spaces, newlines, and tabs, so adding meaningless whitespace in your code is unnecessary and only increases file size.
The following example demonstrates how to use php_strip_whitespace() to remove comments and whitespace from a PHP file:
$filename = 'example.php';
$code = php_strip_whitespace($filename);
echo $code;
This code loads the file example.php and outputs the PHP code stripped of comments and whitespace.
The php_strip_whitespace() function is a useful tool for optimizing PHP code size and improving loading performance. When using it, avoid relying on comments within your code and maintain good formatting practices to maximize optimization benefits.