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

Pong Game in HTML & JavaScript (Updated)

HTML Pong Game Gameplay Pong is one of the first games that many people from the 80s or 90s had played as children. Lots of people know it as a simple arcade game but what they probably do not know is that this simple game helped establish the video game industry! In this post, we'll be making our version of a very simple but fully working Pong game in HTML, CSS and JavaScript. Basic Game Structure Games, however simple or complex they may be, follow the basic Game Loop design as shown in the chart below. Event-oriented game engines usually encapsulate the design and provide you with an event mechanism for handling various parts like the input, update, and rendering but internally the basic design is being followed. Pong Game Loop For our Game Loop, we'll be using JavaScript setInterval so that the game code remains asynchronous and separate. As you may have guessed a while (or any other loop) will freeze the page. For our input, we'll be using onmousemove event to update t