How to display HTML source code

Problem: You want to display HTML on your website so that your visitors can see it. E.g. You might have a tutorial page.

Solution: Use the htmlspecialchars function.

This function will encode all characters in the string passed into it so that the final output will be displayed literally on a HTML page.

For example:

If you just placed the following into a page:

<b>Example Text</b>

You’d just end up with:

Example Text

So, instead use:

<?php
echo htmlspecialchars('<b>Example Text</b>', ENT_QUOTES, 'utf-8');
?>

Then you’ll get output like:

<b>Example Text</b>

No related posts.

Leave Your Response

* Name, Email, Comment are Required