Skip to content
Advertisement

How to insert code as code inside a document

<pre>
      Een PHP script kan overal in een document geplaatst worden. <br>
      een PHP script begint met: <code> ***<?php ?>*** </code>
    </pre>

I’m working on a little project for myself to try and make a documentation page, Now everyone knows the basic echo ‘hello world’ inside of the PHP tags like so:

    <?php 
echo 'hello world'; 
?>

yet i have this in an HTML document and was wondering. Is there a way to display PHP code inside of HTML code? or do i need to approach this differently? When i try this inside of the or tags it won’t display the ‘<‘ from ?php. i have also read on MDN docs and it quotes:

*

“Use the element or, if semantically adequate, the element instead. Note that you will need to escape the ‘<‘ character as ‘<‘ to make sure it is not interpreted as markup.”

*

Any help and advice is truly appreciated.

Advertisement

Answer

You need htmlentities(). As per docs

Convert all applicable characters to HTML entities

So basically you need to do this

<?php
echo htmlentities('<?php echo "hello world"; ?>');
?>

Working example http://sandbox.onlinephpfunctions.com/code/c7629280bdb7368e17f41643fed43675dd95fe89

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