PHP Function Reference

PHP highlight_file() Function



The PHP highlight_file() function is used to syntax highlighting of a file. It 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 the below line to the httpd.conf:

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

Syntax

highlight_file(filename, return)

Parameters

filename Required. Specify the path to the PHP file to be highlighted.
return Required. If set to true it makes this function to return the highlighted code.

Return Value

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.

Example: highlight_file() example

In the example below the highlight_file() function is used to syntax highlighting of the file containing the given script.

<?php
//using the highlight_file() function
highlight_file(__FILE__);
?>

The output of the above code will be similar to (word wrapped for readability):

<code><span style="color: #000000">
<span style="color: #0000BB"><?php<br /><br />highlight_file</span>
<span style="color: #007700">(</span><span style="color: #0000BB">__FILE__</span>
<span style="color: #007700">);<br /></span><span style="color: #0000BB">?></span>
</span>
</code>

❮ PHP Miscellaneous Reference