php – How to write code as example, without executing?

Question:

I'm translating a book to an HTML page, there in the book there are the codes, but how do I make my HTML display my code, HTML, PHP and JavaScript, without executing them?

Answer:

You need to escape the characters that make your tags interpreted as HTML, the < and the > .

For example, instead of:

<div>Teste</div>

use:

&lt;div&gt;Teste&lt;/div&gt;

If you are using PHP, there is a function that does this, htmlentities :

echo htmlentities('<div>Teste</div>');

http://ideone.com/DEZduB

In the case of PHP code, just output it without the initial <?php , and it will not be interpreted. If you want to include <?php in the example, use &lt;?php .

Scroll to Top