Hudzilla.org - the homepage of Paul Hudson
Contents > Writing PHP > Debugging Wish List | Report Bug | About Me ]

19.8.5     Source highlighting: highlight_file() and highlight_string()

This is NOT the latest copy of this book; click here for the latest version.

mixed highlight_file ( string filename [, bool return])

mixed highlight_string ( string filename [, bool return])

An easy way to spot very basic errors is to use a text editor that has syntax highlighting capabilities. Editors like these will recognise that you are editing a PHP script and automatically highlight the text in such a way as to make each element stand out in the source code. We discussed syntax highlighting in earlier, but what I want to mention here is that PHP has built-in support for syntax highlighting itself.

The two key functions here are highlight_file() and highlight_string(), although there is also a function show_source() that is an alias to highlight_file(). Highlight_file() takes a filename as its parameter, and outputs to the screen that file, with all keywords, strings, numbers, and functions highlighted in various colours. Highlight_string() is almost identical, except it takes a string as its parameter.

Author's Note: Many people use these two functions to allow visitors to their site to view the source code for their pages, however it is important to remember that doing so potentially reveals secret information such as database passwords.



This example shows how to highlights a string of code and also a file:

<?php
    $mystr
= '<?php $foo = "bar"; $bar = array("baz", "wombat", "foo"); var_dump($foo); ?>';
    
highlight_string($mystr);
    
file_put_contents("highlighter.php", $mystr);
    
highlight_file("highlighter.php");
?>

As you can see, that passes the string into highlight_string(), then saves it out as highlighter.php and passes that filename into highlight_file() to print out again. Note that both highlight_string() and highlight_file() can take a second parameter, which, if set to true, will make these functions return the highlighted HTML rather than just print it out directly, which gives you a little more control over it.





<< 19.8.4 Triggering your own errors: trigger_error()   19.8.6 Handling MySQL errors: mysql_error() >>
Table of Contents
Want to see this stuff in print? PHP in a Nutshell takes the core topics covered here, adds in thousands of edits from the editorial team and myself, and combines them to make an unbeatable reference for PHP programmers at all levels.



My latest book has hundreds more tips on how to use PHP, Apache, and MySQL, plus Perl, Python, shell scripts, performance tuning, and more!



Top-right shadow
 
Bottom-left shadow Bottom shadow

Comments from other readers
Be the first to add a comment to this chapter!



Add comment
Please note that by posting a comment here you are committing it to the public domain. This is important so that others can make use of your code themselves, and also so that I can incorporate helpful notes directly into the main text. Comments are limited to 2000 characters in length.

If you are reporting an error in the content, please tell me directly.

Your name/email address:
Your comment:
 
Now, in order to verify that you're a real person, please answer this simple question: what is three plus one?
The answer is:
(please write in
numbers, eg 19)


Top-right shadow
 
Bottom-left shadow Bottom shadow