Skip to main content

How Do I Color-Highlight PHP Source Codes

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.

PHP Source Code Highlighting - Using .phps Files

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');

?>

PHP Source Code Highlighting  - Using highlight_file() Function

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:

PHP Source Code Highlighting  - Using highlight_string() Function

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:

Popular posts from this blog

The Best Way(s) to Comment out PHP/HTML Code

PHP supports various styles of comments. Please check the following example: <?php // Single line comment code (); # Single line Comment code2 (); /* Multi Line comment code(); The code inside doesn't run */ // /* This doesn NOT start a multi-line comment block /* Multi line comment block The following line still ends the multi-line comment block //*/ The " # " comment style, though, is rarely used. Do note, in the example, that anything (even a multi-block comment /* ) after a " // " or " # " is a comment, and /* */ around any single-line comment overrides it. This information will come in handy when we learn about some neat tricks next. Comment out PHP Code Blocks Check the following code <?php //* Toggle line if ( 1 ) {      // } else {   ...

How to Create an HTML Form Linked with MySQL Database in PHP

If you're looking for example code and detailed discussion on how to create an HTML form that stores data in a MySQL database using PHP then this post might be what you're looking for. I assume that you're familiar with basic HTML, CSS, PHP coding, and  MySQL. I am going to divide this small project into two parts: The HTML form itself that takes input from the user and the PHP script that saves it into the database A table that displays the user-added data that has been saved in the database. We'll be dealing with the first part in this tutorial. Again I'd like to break this problem into a few parts so that it's easier for you to understand and probably gives you an insight into how breaking up a problem into smaller chunks can help make things clearer in your mind. Let's think about it, there is an HTML form (that is HTML code), then there is the PHP code that deals with the user-input data, and the MySQL database itself. For this tutorial, we'll b...

Fix For Toshiba Satellite "RTC Battery is Low" Error (with Pictures)

RTC Battery is Low Error on a Toshiba Satellite laptop "RTC Battery is Low..." An error message flashing while you try to boot your laptop is enough to panic many people. But worry not! "RTC Battery" stands for Real-Time Clock battery which almost all laptops and PCs have on their motherboard to power the clock and sometimes to also keep the CMOS settings from getting erased while the system is switched off.  It is not uncommon for these batteries to last for years before requiring a replacement as the clock consumes very less power. And contrary to what some people tell you - they are not rechargeable or getting charged while your computer or laptop is running. In this article, we'll learn everything about RTC batteries and how to fix the error on your Toshiba Satellite laptop. What is an RTC Battery? RTC or CMOS batteries are small coin-shaped lithium batteries with a 3-volts output. Most laptops use ...