W3docs

highlight_file()

In this article, we will focus on the PHP highlight_file() function. We will provide you with an overview of the function, how it works, and examples of its

In this article, we will focus on the PHP highlight_file() function. We will provide you with an overview of the function, how it works, and examples of its use.

Introduction to the highlight_file() function

The highlight_file() function is a built-in PHP function that prints a PHP source file with its syntax color-coded as HTML. It is handy when you want to show the contents of a script on a web page — for example in a tutorial, a code snippet gallery, or simple documentation — without writing your own highlighter.

Syntax

highlight_file(string $filename, bool $return = false): string|bool
  • $filename — the path to the PHP file you want to display.
  • $return — when false (the default) the highlighted HTML is sent straight to the output buffer and the function returns true. When true, the HTML is returned as a string instead of being printed.

Under the hood, PHP tokenizes the file and wraps each token (keywords, strings, comments, etc.) in <span> elements with inline colors taken from the highlight.* directives in php.ini.

Security note: highlight_file() reveals the raw source of a file, including any credentials, API keys, or database passwords written in it. Never point it at a file based on unvalidated user input, and never expose it for files that contain secrets.

How to use the highlight_file() function

Using the highlight_file() function is very simple. You just need to call the function and pass the filename of the PHP file that you want to highlight. Here is an example:

How to use the highlight_file() function?

<?php
$file = 'example.php';
highlight_file($file);
?>

In this example, $file holds the path of the script we want to display. If example.php contains:

<?php
$name = "World";
echo "Hello, $name!";

calling highlight_file($file) prints HTML similar to this (colors come from inline style attributes, trimmed here for readability):

<pre><code style="color: #000000"><span style="color: #0000BB">&lt;?php
$name </span><span style="color: #007700">= </span><span style="color: #DD0000">"World"</span><span style="color: #007700">;
echo </span><span style="color: #DD0000">"Hello, $name!"</span><span style="color: #007700">;</span></code></pre>

The function already wraps everything in a <pre><code> block, so the original line breaks and indentation are preserved in the browser without any extra markup from you.

Customizing the output

The highlight_file() function has limited built-in customization options. It only accepts a second boolean parameter $return. When set to true, the function returns the highlighted HTML as a string instead of outputting it directly to the browser.

This is useful when you want to post-process the markup, cache it, or embed it inside a larger template instead of printing it immediately. Here is an example of how to capture the output:

Example of capturing output

<?php
$file = 'example.php';
$highlighted = highlight_file($file, true);

// $highlighted is already a complete <pre><code>...</code></pre> block,
// so you can store it, insert it into a template, or echo it directly.
echo $highlighted;
?>

You can change the colors globally by setting the highlight.comment, highlight.default, highlight.html, highlight.keyword, and highlight.string directives in php.ini (or at runtime with ini_set()), but the function always emits inline styles — there is no way to make it output CSS classes.

If you need advanced customization (custom CSS classes, line numbers, or different color schemes), highlight_file() is not the right tool:

  • To highlight code that lives in a string variable rather than a file, use highlight_string().
  • To read a file's contents without highlighting, use readfile() or file_get_contents().
  • For full control over the output, build a custom highlighter on top of PHP's token_get_all(), or use a third-party library such as highlight.js or Prism on the front end.

Conclusion

In conclusion, the highlight_file() function is a straightforward tool for generating formatted PHP code with syntax highlighting. By understanding its actual parameters and limitations, you can effectively use it for basic syntax display or integrate it with external tools for more advanced formatting needs.

Practice

Practice
What does the highlight_file() function in PHP do?
What does the highlight_file() function in PHP do?
Was this page helpful?