How to echo php code by php itself - htmlspecialchars - nl2br
Topics

--><!-- How to easily echo even php code via php itself-->

<h1>How to echo php-code in php</h1>

<p>You can include the file via <i>include ($path_to_file);</i> Then the php code will be executed. This will be so even if the file ends with ".txt".
If you want to output the code itself you have to use this:
<br>

$code = nl2br (htmlspecialchars(file_get_contents("$path_to_file")));
<br>

echo ($code);
<br>
htmlspecialchars is a special function because it converts all angle brackets into entities. For example
<br>
<h1>header</h1>

will be converted to
&lt;h1&gt;header&lt;/h1&gt;
<br>
Be careful not to grant access to php-files containing passwords. On "include" see <a href="http://www.primitivecode.com/index.php?topic=-how+to-include+or+file_get_contents+in+php-">this</a>.
<br>

</p>


<br>

<p>

To see how php handles nl2br you have to look at the source by clicking "sneak peak", but you also have to
open it directly in the browser to see what the php code acually does:
<br>
<br>
<img src="../images/nl2br.png" width="333" height="415" alt="" border="0">
<br>
<br>
I entered it into the editor like this:
<br>
<br>
<img src="../images/nl2br2.png" width="633" height="652" alt="" border="0">
<br>
<br>
<?php

$test = nl2br ("this is a text with
a
lot
of
breaks
that
have
been
entered
into
the
code
editor.




");

echo $test;

?>

</p>



<br>

<?php $Weblog->share ();?>


<br>
<br>
<br>





Sneak peek of the server side code