highlight_string
(PHP 4, PHP 5, PHP 7, PHP 8)
highlight_string — Syntax highlighting of a string
Description
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
stringThe PHP code to be highlighted. This should include the opening tag.
returnSet this parameter to
trueto 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
| Version | Description |
|---|---|
| 8.4.0 | Return type changed from string|bool to string|true. |
| 8.3.0 | The resulting HTML has changed. |
Examples
Example #1 highlight_string() example
<?php
highlight_string('<?php phpinfo(); ?>');
?>The above example will output:
<code><span style="color: #000000"> <span style="color: #0000BB"><?php phpinfo</span><span style="color: #007700">(); </span><span style="color: #0000BB">?></span> </span> </code>
Output of the above example in PHP 8.3:
<pre><code style="color: #000000"><span style="color: #0000BB"><?php phpinfo</span><span style="color: #007700">(); </span><span style="color: #0000BB">?></span></code></pre>
Notes
Note:
When the
returnparameter is used, this function uses internal output buffering so it cannot be used inside an ob_start() callback function.
The HTML markup generated is subject to change.
See Also
- highlight_file() - Syntax highlighting of a file
- Highlighting INI directives