如何在 HTML 中显示 PHP 代码?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4842575/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 14:32:32 来源:igfitidea点击:
How do I display PHP code in HTML?
提问by álvaro González
How do I display PHP code in HTML?
如何在 HTML 中显示 PHP 代码?
回答by Mark Baker
highlight_file('myFile.php');
or change the file extension to phps
或将文件扩展名更改为 phps
回答by álvaro González
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 有一个内置函数来生成语法高亮:
<?php
$code = '<?php
echo "Hello, World!";
?>';
highlight_string($code);
?>
回答by phihag
Replace all occurences of <
with <
(and optionally >
with >
):
替换所有出现的<
with <
(和可选地>
with >
):
<pre>
<?php echo 'hello world'; ?>
</pre>
回答by Darin Dimitrov
You could use the <pre>
tag:
你可以使用<pre>
标签:
<pre>SOME HTML ENCODED PHP CODE</pre>