This page links to several examples of PHP syntax-highlighting referred to in the article, 5 Easy Ways to Display Syntax Highlighted PHP Code.
Using the PHP function highlight_file(), we can generate “a syntax-highlighted version of the given PHP code using the colors defined in the built-in syntax highlighter for PHP.”
.phps extension.php to .phps. Be careful not to reveal the source of any secure data, such as passwords, etc.FILE constanthighlight_file() function at the beginning of the file. The first line in this example is the only code needed to highlight any PHP document.Using the PHP function highlight_string(), we can generate “a syntax-highlighted version of the given PHP code using the colors defined in the built-in syntax highlighter for PHP.”
?php highlight_string('<?php phpinfo(); ?>'); ?>, we get this output:
<?php phpinfo(); ?>
. The result is syntax-highlighted output of the input string. Note that the output looks better (i.e., as intended) on a white background.highlight_string requires us to escape quotes (e.g., ', "). Depending on your input string, escaping e\v\e\r\y quote may prove tedious and boring, opening wide the doors to potential error. Fortunately, the following technique eliminates the frustration by cleaning things up automagically:?php code(); ?>$string = '[ place your "highly quoted" PHP string here ]';?php code(); ?>?function code() {static $on = false;if ( !$on ) {ob_start();} else {$buffer = "<?\n" . ob_get_contents() . "?>";ob_end_clean();highlight_string( $buffer );}$on = !$on;}?>