David David - 2 months ago 6
PHP Question

Displaying PHP Code as Plaintext

I need to do something like this:

header("Content-Type: text/plain");
echo <<<EOT
<?php echo 'arbitrary code using ' . $variables . ' and such.';
echo 'finished';
?>
EOT;


The problem is, PHP still interprets the inline PHP as code and tries to execute it. I would like just to see the code printed in the window.

Answer

Use Nowdoc, notice the quotes around 'EOT':

echo <<<'EOT'
<?php echo 'arbitrary code using ' . $variables . ' and such.'; 
echo 'finished';
?>
EOT;

Or use a single quoted string, obviously escaping single quotes in the string:

echo '
<?php echo \'arbitrary code using \' . $variables . \' and such.\'; 
echo \'finished\';
?>';
Comments