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

How to echo php-code in php

You can include the file via include ($path_to_file); 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:
$code = nl2br (htmlspecialchars(file_get_contents("$path_to_file")));
echo ($code);
htmlspecialchars is a special function because it converts all angle brackets into entities. For example

header

will be converted to <h1>header</h1>
Be careful not to grant access to php-files containing passwords. On "include" see this.


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:



I entered it into the editor like this:



this is a text with
a
lot
of
breaks
that
have
been
entered
into
the
code
editor.





submit to reddit





Sneak peek of the server side code