Highlight PHP source in HTML

August 30th, 2005 | Filed under PHP, Code snippets, HTML

PHP’s highlight_string function formats and colorcodes PHP source in HTML, which will come in very handy for publishing code snippets on this site.

For example, a simple class called Greeter echoes “Hello, $x” to the screen. It’s source code is formatted with colors and indentation for HTML like this using highlight_string:

Here’s Greeter’s output, and here’s the source view.

highlight_string [PHP manual via Beginner’s PHP]