PHP highlight_file() Function

The highlight_file() function outputs a file with the PHP syntax highlighted. The syntax is highlighted by using HTML tags.

mixed highlight_file ( string $filename [, bool $return = FALSE ] )

Prints out or returns a syntax highlighted version of the code contained in filename using the colors defined in the built-in syntax highlighter for PHP.

Many servers are configured to automatically highlight files with a phps extension. For example, example.phps when viewed will show the syntax highlighted source of the file. To enable this, add this line to the httpd.conf:

AddType application/x-httpd-php-source .phps

Example -

Using a test file ("test.php") to output the file with the PHP syntax highlighted:

The browser output of the code above could be (depending on the content in your file):

The HTML output of the code above could be (View Source):

ParameterDescription
filenamePath to the PHP file to be highlighted.
returnOptional. If this parameter is set to TRUE, this function will return the highlighted code as a string, instead of printing it out. Default is FALSE

Return Values

If return is set to TRUE, returns the highlighted code as a string instead of printing it out. Otherwise, it will return TRUE on success, FALSE on failure.