If you’ve been a regular reader of this blog you might have wondered how I highlight PHP code using different colors. Your prompt answer would be, using some software. Well not exactly as PHP has built-in feature for highlighting source code. And of course I use that.
Here are some of the different methods that you can use to highlight PHP source code, you may use any one of them depending upon what your intended purpose is.
1. If have set-up PHP as explained in Configuring Apache Web Server and PHP then you can just name your PHP files like “filename.phps” to tell PHP to highlight and show the file upon request and not execute it. You can use this method when you want to link the highlighted source from some web page. You cannot, however, embed source code into web pages using this method.
2. You can use the following function to highlight any PHP (.php) script file:
highlight_file('filename.php');
You just have to pass the filename to this function and PHP will highlight and output the code to the browser.
Main benefit with this is you can embed source code into your web pages wherever you want.
e.g.:
<?php
echo "<h1>Blah Blah Blog</h1>";
echo "<h2>Highlighting PHP Source Code</h2>";
echo "<p>blah blah blah!!</p>";
echo "<p>Write anything you want and embed source code (highlighted)</p>";
echo "<p>Embedded source code is listed below:</p>";
highlight_file('highlight_file.php');
?>
3. There are times when you just want to highlight a single line of code which can be done using:
highlight_file('string
here');
It could be used when you have lost of unrelated lines of code to highlight. In other words, when the codes are not together but are sprinkled throughout the page. For this you don’t have to create any files to be highlighted, just use this function with thee source code string.
e.g.:
<?php
echo "<h1>Blah Blah Blog</h1>";
echo "<h2>Highlighting PHP Source Code</h2>";
echo "<p>First String: <br />";
highlight_string("<?php echo \"<h1>Blah Blah Blog</h1>\"; ?>");
echo "<br />Second String: <br />";
highlight_string("<?php foreach($keywords as $keyword) ?>");
echo "</p><p>blah blah blah blah <br />blah blah blah</p>";
echo "<p>Third highlighted String: <br />";
highlight_string("<?php if(isset($s)) ?>");
echo "<p>Notice how many unrelated code strings are here. The best way to format them in this condition is to use ";
highlight_string("<?php highlight_string(\"string\"); ?>");
echo "function </p>";
?>
Which would show up like below:
One interesting thing that you can play with, regarding syntax highlighting is the “php.ini” file. Look for these lines in that file:
; Colors for Syntax Highlighting mode. Anything that's acceptable in
; <span style="color: ???????"> would work.
highlight.string = #DD0000
highlight.comment = #FF9900
highlight.keyword = #007700
highlight.bg = #FFFFFF
highlight.default = #0000BB
highlight.html = #000000
Here, as you can see, the colors for highlighting different parts of the code are defined. You can change these to alter the colors that are use for highlighting. Colors are in HEX RGB format (same as HTML color code).
NOTE: If you don’t seem to be able to see the source code highlighted you might need remove the ";" (semi-colons) from the starting of each of the "highlight." line so it looks like the above code:
; Colors for Syntax Highlighting mode. Anything that's acceptable in
; <span style="color: ???????"> would work.
;highlight.string = #DD0000
;highlight.comment = #FF9900
;highlight.keyword = #007700
;highlight.bg = #FFFFFF
;highlight.default = #0000BB
;highlight.html = #000000
If only “.phps” PHP source files are not getting highlighted, you’ll need to set-up PHP as described in Configuring Apache Web Server and PHP.
Previous Posts: