PHP Function Reference

PHP highlight_string() Function



The PHP highlight_string() function is used to syntax highlighting of a string. It prints out 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.

Syntax

highlight_string(string, return)

Parameters

string Required. Specify the PHP code to be highlighted. This should include the opening tag.
return Required. If set to true it makes this function to 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 on success, false on failure.

Example: highlight_string() example

The example below shows the usage of highlight_string() function.

<?php
//the PHP code which need to be highlighted
$str = "<?php phpinfo(); ?>";

//using the highlight_string() function
highlight_string($str);
?>

The output of the above code will be similar to (word wrapped for readability):

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

❮ PHP Miscellaneous Reference