PHP highlight_file() function

Definition and Usage

The highlight_file() function highlights the syntax of a file.

Syntax

highlight_file(filename,return)
Parameters Description
filename Required. The path to the PHP file to be highlighted.
return Optional. If set to true, this function returns the highlighted code.

Description

This function outputs or returns the highlighted code contained in filename version.

many servers are configured to highlight the syntax of code in phps to automatically highlight files with the suffix. For example, when viewing example.phps, the source code with syntax highlighting will be displayed. To enable this feature, add the following line to httpd.conf:

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

return value

if return If the parameter is set to true, the function will return the highlighted code instead of outputting it. Otherwise, if successful, it returns true, and if failed, it returns false.

Tips and Comments

Warning:It should be noted that when using the highlight_file() function, do not inadvertently leak sensitive information such as passwords or other types of sensitive information, otherwise potential security risks may occur.

Example

"test.php":

<html>
<body>
<?php
highlight_file("test.php");
?>
</body>
</html>

Output:

<html> 
<body> 
<?php 
highlight_file("test.php");
?>
</body> 
</html>

The result viewed in the browser is similar to this:

<html>
<body>
<code>
<span style="color: #000000"><html>
<br />
<body>
<br />
<span style="color: #0000BB"><?php
<br />highlight_file</span>
<span style="color: #007700">(</span>
<span style="color: #DD0000">"test.php"</span>
<span style="color: #007700">);<br /></span>
<span style="color: #0000BB">?><br /></span>
</body>
<br />
</html></span>
</code>
</body>
</html>