You are here

Code Filter

You might consider using add on module Code Filter if you write PHP code or want to write about PHP on your Web site. Code Filter displays code with pretty formatting and the correct indentation.

Install Code Filter. Log on as administrator, go to administer > modules and switch on Code Filter. Visit administer > input formats, find the line for Full HTML, select configure, and switch on Code filter.

Experiment with PHP and other code in your next article. The following examples use PHP code from one of my books on PHP. This page uses the default Drupal Full HTML input format and displays the code unmodified plus provides highlighted using the html <code> element. Drupal also provides a PHP filter to execute PHP code from within pages. Pages become complicated when you mix execution of PHP code with display of PHP code. Code Filter attempts to provide more options. Code Filter is not installed in this site so there are no examples of using Code Filter.

To make <br /> display as text instead of producing a break, < is replaced by &lt; and > is replaced by &gt;. You make your indents, tabs or spaces, work by using the HTML <pre> element or replacing the tabs and spaces with &nbsp;. The following example uses &nbsp;.
$tracks[] = "Smokestack Lightnin";
$tracks[] = "I'd Rather Go Blind";
shuffle($tracks);
foreach($tracks as $track)
    {
    print $track . '<br />';
    }
?>

The following example uses <pre>.

The core Drupal filter module supports the <code> HTML element. The following code is the first example, the &nbsp; example, wrapped in the code element.
$tracks[] = "Smokestack Lightnin";
$tracks[] = "I'd Rather Go Blind";
shuffle($tracks);
foreach($tracks as $track)
    {
    print $track . '<br />';
    }
?>

The following example uses <code><pre>.