Thursday, October 23, 2025
HomeLanguagesPHP | php_strip_whitespace() Function

PHP | php_strip_whitespace() Function

The php_strip_whitespace() is an inbuilt function in PHP which is used to remove all comments and whitespace from the source code.

Syntax:

string php_strip_whitespace ( $file )

Parameters: The php_strip_whitespace() function accepts a single parameter $file. It is a mandatory parameter which specifies the file.

Return value: It returns the source code of $file after removing the comments and whitespace on success otherwise it returns an empty string.

Exceptions:

  • It works on PHP 5.0.1 and later versions, it returns an empty string in previous versions.
  • This function works similar as php -w command line.

Below programs illustrate the php_strip_whitespace() function in PHP:
Program:




<?php
// Simple program to remove comment
// using php_strip_whitespace function.
   
// function to calculate fibonacci number
function fib($n)
{
    if ($n <= 1)
        return $n;
    return fib($n - 1) + fib($n - 2);
}
   
// Driver Code
$n = 9;
fib($n);
  
/*
* One more multiline comment 
*/
  
echo php_strip_whitespace(__FILE__);
  
// This line also removed 
?>


Output:

<?php
function fib($n) { if ($n <= 1) return $n; 
return fib($n - 1) + fib($n - 2); } $n = 9;
fib($n); echo php_strip_whitespace(__FILE__); 
?>

Reference: http://php.net/manual/en/function.php-strip-whitespace.php

RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS