W3docs

highlight_string()

In this article, we will focus on the PHP highlight_string() 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_string() function. We will cover what it does, its signature and return value, how to capture the output as a string, common gotchas, and when to reach for a different approach.

Introduction to the highlight_string() function

highlight_string() is a built-in PHP function that returns or prints an HTML-highlighted version of a string of PHP source code. It is useful when you want to show readable, color-coded code samples on a web page — for example in documentation, a blog post, or a tutorial site — without writing your own tokenizer.

It is important to understand that highlight_string() only highlights PHP code, and the code must include the opening <?php tag (or a short tag) for the highlighting to kick in. Plain text without PHP tags is largely rendered as a single token, so the colors you expect will not appear.

Signature

highlight_string(string $string, bool $return = false): string|bool
  • $string — the PHP code to highlight.
  • $return — if true, the highlighted HTML is returned as a string; if false (the default), it is echoed directly and the function returns true.

The generated markup uses inline style="color: …" attributes that come from the highlight.* ini settings (such as highlight.string and highlight.keyword).

How to use the highlight_string() function

In its simplest form, call the function and pass the PHP string you want to highlight:

How to use the highlight_string() function?

<?php
$string = '<?php echo "Hello, World!"; ?>';
highlight_string($string);
?>

This prints HTML similar to the following directly to the browser:

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

In the browser, the keywords, string, and tags render in different colors. Notice that the special characters < and > are escaped to &lt; and &gt;, so the highlighted code displays safely as text rather than being interpreted as HTML.

Returning the output instead of printing it

By default highlight_string() echoes the highlighted HTML immediately. Pass true as the second argument when you want to capture the result — for example to cache it, run it through a template, or wrap it in extra markup before sending it to the page:

Example of PHP highlight_string()

<?php
$string = '<?php echo "Hello, World!"; ?>';
$highlighted = highlight_string($string, true);
echo '<pre>' . $highlighted . '</pre>';
?>

Because the function already wraps its output in a <pre><code> block, you usually do not need to add your own <pre> — the wrapper above is shown only to illustrate that $highlighted is an ordinary string you can concatenate with anything.

Gotchas and limitations

  • The code must contain PHP tags. highlight_string('echo "hi";') (no <?php) is treated as plain text and comes out unstyled. Wrap your snippet in <?php … ?> first.
  • The input is not escaped by you — the function does it. highlight_string() HTML-encodes the code itself, so never run it through htmlspecialchars() first or you will see doubled-up entities like &amp;lt;.
  • No options for line numbers or CSS classes. The colors are inline style attributes pulled from the highlight.* ini settings; there is no built-in way to add line numbers or custom classes.
  • It calls the real PHP tokenizer. Syntactically broken code may emit warnings, since the function lexes the input the same way the engine does.

For advanced highlighting — custom CSS classes, line numbers, or highlighting languages other than PHP — parse the code yourself with token_get_all() and build your own markup, or use a dedicated library.

  • highlight_file() — the same highlighter, but reads the PHP code from a file instead of a string.
  • htmlspecialchars() — escape HTML special characters when you are not using highlight_string().
  • htmlentities() — convert all applicable characters to HTML entities.
  • nl2br() — insert <br> tags before newlines when displaying plain text.

Conclusion

In conclusion, the highlight_string() function is a built-in tool for generating formatted PHP strings with syntax highlighting. By understanding how to use the function and its return parameter, you can take advantage of this feature to display readable, highlighted code in your web applications.

Practice

Practice
What is the primary function of the highlight_string() function in PHP?
What is the primary function of the highlight_string() function in PHP?
Was this page helpful?