php_strip_whitespace() function in PHP

The php_strip_whitespace() function returns PHP source code with stripped comments and whitespace, making it useful for code optimization and analysis.

Syntax

php_strip_whitespace(file_path)

Parameters

  • file_path − The path to the PHP file to be processed.

Return Value

The php_strip_whitespace() function returns the stripped source code as a string on success, or FALSE on failure.

Example

Let's create a sample PHP file and then use php_strip_whitespace() to process it ?

<?php
// sample.php content:
/*
 * This is a multi-line comment
 * Demo file for testing
 */

echo "Hello World!";    // Inline comment

   // Extra whitespace and comment
   $name = "PHP";
   echo $name;
?>

Now, let's strip the whitespace and comments ?

<?php
   echo php_strip_whitespace("sample.php");
?>
<?php
echo "Hello World!";$name = "PHP";echo $name;
?>

Key Points

  • Removes all comments (single-line and multi-line)

  • Strips unnecessary whitespace and newlines

  • Preserves string literals and their contents

  • Requires file system access to read the specified file

Conclusion

The php_strip_whitespace() function is useful for code analysis and optimization by removing comments and excess whitespace while preserving the functional PHP code.

Updated on: 2026-03-15T07:37:24+05:30

154 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements