highlight_string()

highlight_string() — Syntax highlighting of a string.

Built-in function of PHP 8.5. The manual lists it as available in PHP 4, PHP 5, PHP 7, PHP 8. This page is the reference card: signature, parameters and return value, plus the changelog and the example the manual provides.

What it does

Outputs or returns html markup for a syntax highlighted version of the given PHP code using the colors defined in the built-in syntax highlighter for PHP.

Signature

highlight_string(string $string, bool $return = false): string|true

Parameters

ParameterTypeWhat it takes
$stringstringThe PHP code to be highlighted. This should include the opening tag.
$returnboolSet this parameter to true to make this function 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.

Example

A runnable snippet:

highlight_string('<?php phpinfo(); ?>');

Output:

<code><span style="color: #000000"> <span style="color: #0000BB">&lt;?php phpinfo</span><span style="color: #007700">(); </span><span style="color: #0000BB">?&gt;</span> </span> </code>

Changelog

VersionChange
8.4.0Return type changed from string|bool to string|true.
8.3.0The resulting HTML has changed.

See also

highlight_file()

All PHP 8.5 built-in functions

Reference card built from the PHP manual page highlight_string() (PHP documentation, CC BY 3.0). The page is generated from the manual, so the wording of the parameter and return value text follows php.net.