Use this function as a quick and easy way to view some PHP code. It will add HTML to it, adding color for better readability.

Note that you do need the opening <?php tag (otherwise, it will think it’s plain text - just like a PHP file would).

By default it prints the result. Set the second parameter to true if you want to capture the resulting string instead.

$str = '<pre>
<php
echo "Hi!";
$name = "Robert";
echo "Hello $name";
?>
 
The End.
';
 
 
$new = highlight_string( $str, true );
/*
<code><span style="color: #000000">
&lt;pre&gt;<br /><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #DD0000">"Hi!"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$name&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"Robert"</span><span style="color: #007700">;<br />echo&nbsp;</span><span style="color: #DD0000">"Hello&nbsp;$name"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;<br /></span><br />The&nbsp;End.<br /></span>
</code>
*/