Question

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.

Was it helpful?

Solution

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

OTHER TIPS

Escape your code with htmlspecialchars():

<div id="code">
    <?php echo htmlspecialchars("<?php echo 'Bla Bla Bla' ?>") ?>
</div>

Take a look here: http://www.php.net/manual/en/function.htmlentities.php

<div id = "code"><?php echo htmlentities('<?php

echo "Bla Bla Bla";
?>'); ?></div>

Try this

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

try using htmlspecialchars

htmlspecialchars()

if i understood your question properly then you can achive it by following..

      <div class="test"><?php $a='<?php hii echo "hey there"?>';?></div>

Solution 1: Your solution would probably be to not name the file .php ... Try .txt in stead. This way the web server would not pass the file to the PHP parser.

Solution 2: Other solution would be to read out the file from another php script that does file() in combination with htmlspecialchars() to the file with the desired code.

Solution 3: If you already have the code in a variable and it outputs in a way that you can see the code in "source view", then you suffice by only using htmlspecialchars()

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top