A couple of simple PHP commands can easily convert any HTML so that it can be displayed in the web page without you having to go through the HTML converting any of it. By using these PHP commands to do the conversion for you there is no possibility of your overlooking any of the characters that need to be converted in order to display the HTML as code.
The first PHP command you need to do this is html_specialchars(). This command will take your HTML content that you want to display in the page as code and will escape all of the characters that would be interpreted as HTML by converting them into entity codes. The second command nl2br() converts all the new line characters in your code into break tags so that they will actually display as separate lines in the web page.
All you need to do then is to wrap the PHP inside of the code tag (or whatever it is you are using to identify the HTML that is to be displayed as code within the page.
<code><?php nl2br(html_specialchars('HTML to display as code')); ?></code>
Note that if you are going to place the content inside a textarea instead of a code tag then you don't need the nl2br call as the linefeeds will be correctly interpreted inside that tag.
This article written by Stephen Chapman, Felgall Pty Ltd.