In PHP development, there are times when we want to display the source code of a PHP file to users, such as when creating educational examples or building an online code demonstration platform. In these cases, the highlight_file() function is particularly useful. It can read and display the contents of a PHP file while automatically adding syntax highlighting, making the code easier to read and understand.
highlight_file() is a built-in PHP function used to read a specified PHP file and output its source code with syntax highlighting. It differentiates keywords, variables, strings, and other elements using different colors to enhance code readability.
<span><span><span class="hljs-title function_ invoke__">highlight_file</span></span><span>(</span><span><span class="hljs-keyword">string</span></span><span> </span><span><span class="hljs-variable">$filename</span></span><span>, </span><span><span class="hljs-keyword">bool</span></span><span> </span><span><span class="hljs-variable">$return</span></span><span> = </span><span><span class="hljs-literal">false</span></span><span>): </span><span><span class="hljs-keyword">string</span></span><span>|</span><span><span class="hljs-keyword">bool</span></span></span>
$filename: The path of the file to read and highlight.
$return (optional): Defaults to false, meaning the highlighted code is output directly. If set to true, the function returns the highlighted code as a string instead of outputting it directly.
Returns true or a string containing the highlighted code on success (when $return is true).
Returns false on failure.
Here’s the simplest way to output a PHP file’s contents with highlighting directly on the page:
<span><span><span class="hljs-meta"><?php</span></span><span>
</span><span><span class="hljs-title function_ invoke__">highlight_file</span></span><span>(</span><span><span class="hljs-string">'example.php'</span></span><span>);
</span><span><span class="hljs-meta">?></span></span><span>
</span></span>
With just one line of code, you can output the example.php file contents with syntax highlighting.
If you want to further manipulate the highlighted content, such as wrapping it in a specific HTML structure, set the $return parameter to true and store the result in a variable:
<span><span><span class="hljs-meta"><?php</span></span><span>
</span><span><span class="hljs-variable">$highlightedCode</span></span><span> = </span><span><span class="hljs-title function_ invoke__">highlight_file</span></span><span>(</span><span><span class="hljs-string">'example.php'</span></span><span>, </span><span><span class="hljs-literal">true</span></span><span>);
</span><span><span class="hljs-keyword">echo</span></span><span> </span><span><span class="hljs-string">"<div class=\"code-block\"><span class="hljs-subst">$highlightedCode</span></span></span><span></div>";</span></span>
</span><span><span class="hljs-meta">?></span></span><span>
</span></span>
This allows you to flexibly embed highlighted code into your website templates or front-end frameworks.
Security: Do not allow users to input filenames directly to highlight_file(), as this can lead to serious security risks, such as path traversal attacks. Always validate against a whitelist.
HTML Structure: The output of highlight_file() contains HTML tags and CSS styles, so ensure your webpage supports HTML output when displaying it.
PHP Files Only: This function only highlights PHP code blocks (i.e., content within <?php ... ?>). Plain text or other languages will not be highlighted.
Writing PHP tutorials or blogs to display code examples.
Online PHP code demonstrators to show source code before execution.
Part of debugging tools in admin systems to display snippets of source code.
highlight_file() is a highly practical built-in PHP function that is easy to use while enhancing the professionalism and aesthetics of code display. Whether for educational demonstrations or building code reading tools, it is very useful. By properly using its two parameters, you can achieve flexible code presentation methods, adding more visual interactivity to your PHP projects.