Display PHP code in a html code section

How do I display PHP code in HTML? you may ask yourself.

Briefly, you cannot print a string with php code directly to a html code block as shown here:

$php_string = <<<CONFIG
<?php
$dbms = 'mysql';
$dbhost = 'localhost';
$dbport = '';
$dbname = '';
$dbuser = '';
$dbpasswd = '';
$table_prefix = '';
$acm_type = 'file';
$load_extensions = '';
CONFIG;
// the heredoc ends here

echo "<html><body><pre><code>". $php_string ."</code></pre></body></html>";

Firstly a php string is initialized, I used a heredoc to write multiple lines to the string. The keyword behind the <<< sequence can be freely chosen. I used CONFIG to give the string some semantic.

Then we’re trying to output the PHP code directly in a code section – but the webpage leaves blank.

There are two problems here. The first one is that nothing is displayed as we already noticed and the second problem is that, after viewing the browser’s page source, the variables have disappeared.

<html><body><pre><code><?php
 = 'mysql';
 = 'localhost';
 = '';
 = '';
 = '';
 = '';
 = '';
 = 'file';
 = '';
</code></pre></body></html>

Problem with heredoc

When using variables inside a heredoc php will parse these and puts their values there. The variables $dbms, $dbhost and all the others were not defined in this case and thus php outputs empty strings.

Instead of a heredoc we should use a nowdoc which is specified similarly to a heredoc, but no parsing is done inside a nowdoc. The construct is ideal for embedding PHP code or other large blocks of text without the need for escaping.

A nowdoc is identified with the same <<< seqeuence used for heredocs, but the identifier which follows is enclosed in single quotes <<<’CONFIG’.

$php_string = <<<'CONFIG'
<?php
$dbms = 'mysql';
$dbhost = 'localhost';
$dbport = '';
$dbname = '';
$dbuser = '';
$dbpasswd = '';
$table_prefix = '';
$acm_type = 'file';
$load_extensions = '';
CONFIG;

Note: Nowdoc support was added in PHP 5.3.0.

Problem with displaying PHP code

The trick is to replace all occurrence of < by &lt;. Only then the PHP code will be displayed on your webpage. Additionally you can do the same with > but this is irrelevant to the rendering.

htmlspecialchars

echo "<html><body><pre><code>";
echo htmlspecialchars($php_string);
echo "</code></pre></body></html>";

Convert special characters to HTML entities.

htmlentities

echo "<html><body><pre><code>";
echo htmlentities($php_string);
echo "</code></pre></body></html>";

This function is identical to htmlspecialchars() in all ways, except with htmlentities(), all characters which have HTML character entity equivalents are translated into these entities.

highlight_string

echo "<html><body><pre><code>";
echo highlight_string($php_string, true);
echo "</code></pre></body></html>";

Outputs or returns a syntax highlighted version of the given PHP code using the colors defined in the built-in syntax highlighter for PHP. It's important that the second argument is defined. This is a boolean argument which defaults to false inside the highlight_string() function. This argument specifies whether to output the string directly or to return the formatted code as a string.

Outputs or returns a syntax highlighted version of the given PHP code using the colors defined in the built-in syntax highlighter for PHP. The second argument specifies whether to output the formatted code directly or to return it as a string. By default this argument is set to false which makes the function output the formatted code directly.

When you ommit this argument you shouldn't use echo to print the formatted code as this would lead to the additional output of a '1' otherwise.

...
highlight_string($php_string);
...

Now let's have a look at the actual output of this function. This is not the browser's page source but what we see in the browser window itself.


<?php
$dbms 
'mysql';
$dbhost 'localhost';
$dbport '';
$dbname '';
$dbuser '';
$dbpasswd '';
$table_prefix '';
$acm_type 'file';
$load_extensions '';

highlight_file

If you want to output highlighted source code from a php file you can use highlight_file.

echo "<html><body><pre><code>";
echo highlight_file("", true);
echo "</code></pre></body></html>";

This works similarly to highlight_string.

Many servers are configured to automatically highlight files with a phps extension. For example, example.phps when viewed will show the syntax highlighted source of the file. To enable this, add this line to the httpd.conf:

AddType application/x-httpd-php-source .phps

2 Comments

  1. Inyavic

    This is a really nice tutorial on Displaying PHP code in a html code section. I’ve had some more looks at your other articles about programming but also the Linux stuff and already learnt a couple of new things since I’m yet a beginner in these fields. Expecting more from you.

  2. How To Connect a MySQL database to a website?

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>