jump to navigation

How to Display PHP-code into a Web-page November 5, 2011

Posted by Tournas Dimitrios in PHP.

PHP has built-in support for syntax highlighting itself and display PHP-code into web-pages . This functionality is useful when php-code snippets need to displayed in an article . highlight_file() is used to display a file with PHP syntax highlighting  .  The function returns TRUE on success and FALSE on failure . The colors used for highlighting can be set in the php.ini file . They can also be set using the ini_set() function

# Show only the comments
ini_set ('highlight.string', '#FFFFFF');
ini_set ('highlight.comment', '#FF8000');
ini_set ('highlight.keyword', '#FFFFFF');
ini_set ('highlight.bg', '#FFFFFF');
ini_set ('highlight.default', '#FFFFFF');
ini_set ('highlight.html', '#FFFFFF');

This is a practical example how to display PHP-code . Be carefull not to reveal sensitive information (username / password ) when using this functionality :

// Define an array of allowed files - VERY IMPORTANT! 
$allowed = array('json.php','Validation.php','Form.php',
if (isset($_GET['view']) && in_array($_GET['view'], $allowed)) 
 $location = './';
 $dir = dir($location);
 while ($entry = $dir->read())
 if (in_array($entry, $allowed))
 echo '<a href="' . $_SERVER['PHP_SELF'] .
 '?view=' . $entry . '">' . $entry . "</a><br />\n";


No comments yet — be the first.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s