Reputation:
How do I display PHP code in HTML?
Upvotes: 11
Views: 50094
Reputation: 146330
Just 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>
Optionally, PHP has a builtin function to generate syntax highlight:
<?php
$code = '<?php
echo "Hello, World!";
?>';
highlight_string($code);
?>
Upvotes: 21
Reputation: 212402
highlight_file('myFile.php');
or change the file extension to phps
Upvotes: 18
Reputation: 1038710
You could use the <pre>
tag:
<pre>SOME HTML ENCODED PHP CODE</pre>
Upvotes: -3
Reputation: 287755
Replace all occurences of <
with <
(and optionally >
with >
):
<pre>
<?php echo 'hello world'; ?>
</pre>
Upvotes: 7