During application development, the need often arises to display source code directly in the browser, but in a way that is readable (with colors). PHP has built-in tools for this that automatically recognize keywords, strings, and comments, highlighting them accordingly.
| Function | Description | Example |
|---|---|---|
highlight_file() |
Reads a file and outputs its content with highlighted PHP syntax. | highlight_file("test.php"); |
highlight_string() |
Takes a string and outputs it with highlighted PHP syntax. | highlight_string($string); |
php_strip_whitespace() |
Returns the code from a file without comments and whitespace. | Code minification |
The highlight_file() and highlight_string() functions use PHP's built-in color
schemes. This is extremely useful if you want to display code examples on your website without having to manually
write <span> tags for each color.
When code goes into production, we sometimes want to reduce file sizes. This function removes all comments and empty lines, making the code less readable for humans but faster for the compiler to parse.