How to display PHP code in HTML?

How to display PHP code in HTML?

+7
source share
4 answers
highlight_file('myFile.php'); 

or change the file extension to phps

+16
source

Like any other piece of text:

 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head><title></title> </head> <body> <?php $code = '<?php echo "Hello, World!"; ?>'; echo '<pre>' . htmlspecialchars($code) . '</pre>'; ?> </body> </html> 

At will, PHP has a built-in function for generating syntax highlighting :

 <?php $code = '<?php echo "Hello, World!"; ?>'; highlight_string($code); ?> 
+13
source

Replace all occurrences of < with &lt; (and optional > with &gt; ):

 <pre> &lt;?php echo 'hello world'; ?&gt; </pre> 
+7
source

You can use the <pre> :

 <pre>SOME HTML ENCODED PHP CODE</pre> 
0
source

All Articles