Highlight PHP Code With the PHP Function Highlight_File and Highlight_String
PHP code can be a lot easier to read when it's syntax highlighted. PHP includes a couple of functions that do this, highlight_file and highlight_string. Both function take a filename and return the file contents with PHP syntax highlighted. These functions are very handy for displaying PHP code on a website.
Both function have a second parameter that when set to true will cause them to return the highlight_string instead of printing it out. Alternatively, both functions can be used with the
When using these PHP code highlighting functions, it's important to remember that revealing the source of your script may reveal sensitive information like passwords, salts/hash, user logins and other types of data that could create a security risk. Therefore, a bit of caution and common sense should be taken when displaying source code.
Although the PHP syntax highlighting functions can be useful, there are also several third-party code highlighters available that will allow you to highlight a wider range of programming languages, including JavaScript and HTML. These are a bit more complicated to use than the built-in PHP highlighting functions, but they offer more options and features. For example, Aidan Lister's code highlighter, GeSHi, supports over 35 different programming languages and can highlight a wide variety of HTML elements in addition to PHP.