highlight_string

Outputs or returns HTML markup showing the given PHP code with syntax highlighting.

Signature
highlight_string(string $string, bool $return = false): string|true
  • Added in PHP 4.0

Parameters

ParameterTypeDescription
$stringstringThe PHP code to highlight. It must include an opening tag or it will be treated as inline HTML and left uncoloured.
$returnoptionalbool= falseWhen true the HTML is returned instead of being printed.

Return value

string|trueThe highlighted HTML when return is true; otherwise the markup is echoed and true is returned.

Errors

Does not throw. Code that fails to parse is still highlighted as best it can be rather than raising an error.

Changelog

  • PHP 8.4.0The return type changed from string|bool to string|true; the function no longer has a false return path.
  • PHP 8.3.0The generated HTML changed: the whole output is now wrapped in a <pre> tag, the outer <span> has been merged into the <code> element, whitespace between the outer HTML tags is removed, and newlines and spaces are no longer converted to HTML entities. (Per the PHP 8.3 UPGRADING notes; the pre-8.3 markup was already <code> plus <span style="..."> elements, not <font> tags.)

The markup is not stable API. Before PHP 8.3 the output was a <code> element wrapping an outer <span style="...">, with newlines and spaces converted to HTML entities; PHP 8.3 wraps the whole thing in <pre>, merges that outer <span> into <code style="..."> and stops entity-encoding whitespace, so any CSS or regex written against the old output breaks on upgrade. (PHP 8.5 emits, for example,

<span style="color: #0000BB"><?php $a …
.)

Example

Try it yourself
Loading editor…
Press Run to execute the code.

When to use it

  • Rendering a code snippet on an internal documentation or error page without a JS highlighter.
  • Displaying a generated migration or config file for review in an admin panel.
  • Turning a small PHP example stored in a database into coloured HTML for a CMS.

Frequently asked questions

What is the difference between highlight_string and highlight_file?

highlight_string() highlights PHP code you pass as a string, while highlight_file() reads the code from a file path first. Both produce the same markup.

Why is my code not highlighted?

The string must start with a PHP opening tag. Without <?php the tokenizer treats the whole snippet as inline HTML and applies no colours.

Can I style the output with CSS?

Not easily, because the colours are inline style attributes generated from the highlight.* ini settings. Change those ini values, or post-process the HTML, since the exact markup is not guaranteed between PHP versions.

More examples

Missing opening tag means no colours

The tagged snippet produces several coloured span elements while the untagged one produces almost none, and the var_dump confirms it with bool(true).

Example · php
<?php
$withTag = highlight_string('<?php $a = 1;', true);
$withoutTag = highlight_string('$a = 1;', true);

echo "with tag:    ", $withTag, "\n";
echo "without tag: ", $withoutTag, "\n";
var_dump(substr_count($withTag, '<span') > substr_count($withoutTag, '<span'));

Print mode versus return mode

With return set to true you get the HTML as a string and nothing is printed; with the default the markup goes straight to output and the call itself yields true.

Example · php
<?php
$code = '<?php echo "hi";';

$result = highlight_string($code, true);
echo "\nreturned ", strlen($result), " bytes of HTML\n";

$printed = highlight_string($code);
echo "\nprint mode returned: ", var_export($printed, true), "\n";

Discussion

  • Be the first to comment on this lesson.