Skip to content
Advertisement

Output PHP Code in HTML, literally

My question is very simple. All I want to do is write actual PHP code in HTML, without executing it. For example, I want to write

<div id = "code"><?php
echo 'Bla Bla Bla';
?></div>

I DO NOT want to execute this PHP. I want to literally write the PHP code on the html document, by somehow escaping the <?php tags (I don’t want to remove them).

Unfortunately, when I echo it to the browser by doing this:

<div id = "code"><?php echo '<?php
echo "Bla Bla Bla";
?>' ?></div>

I have also tried to put the code into variables, and even that hasn’t worked.

My PHP code gets commented out by the browser. I can see it in the View Page Source option in chrome, but the browser thinks it’s a comment.

Please provide a solution to my problem.

Thanks in advance.

PS: I am using CodeMirror to output embedded PHP/HTML code using the ‘application/x-httpd-php’.

EDIT #1

I can use lt and rt for tags, but I actually can’t because CodeMirror starts highlighting code only when it finds <? tags.

Advertisement

Answer

Encode your PHP tags by using &lt;?php echo "test"; ?&gt;.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement