php8.5
Home/ Manual/ misc / functions/ highlight_string

highlight_string

PHP function Edit on GitHub ✎

(PHP 4, PHP 5, PHP 7, PHP 8)

Syntax highlighting of a string

Description

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

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.

Parameters

string

The PHP code to be highlighted. This should include the opening tag.

return

Set this parameter to true to make this function return the highlighted code.

Return Values

If return is set to true, returns the highlighted code as a string instead of printing it out. Otherwise, it will return true.

Changelog

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

Examples

highlight_string() example

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

The above example will output:

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>

83

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

Notes Uses-ob

The HTML markup generated is subject to change.

See Also

Source: reference/misc/functions/highlight-string.xml · from the official PHP manual (php/doc-en)