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

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

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 {      // } //*/ //* Toggle line if ( 2 ) {      // } else {      // } //*/ Now see how easy it is to toggle a part of PHP code by just removing or adding a single " / " from th

Introduction to Operator Overloading in C++

a1 = a2 + a3; The above operation is valid, as you know if a1, a2 and a3 are instances of in-built Data Types . But what if those are, say objects of a Class ; is the operation valid? Yes, it is, if you overload the ‘+’ Operator in the class, to which a1, a2 and a3 belong. Operator overloading is used to give special meaning to the commonly used operators (such as +, -, * etc.) with respect to a class. By overloading operators, we can control or define how an operator should operate on data with respect to a class. Operators are overloaded in C++ by creating operator functions either as a member or a s a Friend Function of a class. Since creating member operator functions are easier, we’ll be using that method in this article. As I said operator functions are declared using the following general form: ret-type operator#(arg-list); and then defining it as a normal member function. Here, ret-type is commonly the name of the class itself as the ope