如何在浏览器中的HTML页面上显示PHP代码?
如何用 PHP 编写 PHP 代码?我想这样做,但它不起作用:
<?php echo '<?php echo \'aoeu\'; ?>'; ?>
希望有人能给我提示,
非常感谢
How can I write PHP code in PHP? I want to do this, but it doesn't work:
<?php echo '<?php echo \'aoeu\'; ?>'; ?>
Hope someone can give me a hint,
Many thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(14)
如果你输出一些复杂的代码,一定要使用像 smarty 这样的模板引擎。否则你的代码看起来会一团糟。
我曾经修补过 propel ORM,它在不使用模板引擎的情况下输出 PHP 代码。它根据 XML 配置文件生成所有模型类。他们的代码一团糟。不要这样做。
If you output some complex code definitely use a template engine like smarty.. otherwise your code will look a complete mess.
I once patched the propel ORM which does output PHP code without using a template engine. It generates all the model classes based on the XML configuration files. Their code was a big mess. Don't do it.
试试这个
<<<是赫里多克
Try This
<<< is Heredoc
如果你不转义 '<'和“>”它们将被打印为 html 标签。
If you don't escape the '<' and '>' they will be printed as html tags.
如果您使用highligh_file,则可以使用highlight_string或highlight_file,您必须指示要显示的php文件
,或者您可以将两者结合起来,这对我来说是最好的解决方案:
或者您可以以不同的方式组合:
You can use highlight_string or highlight_file if you use highligh_file you have to indicate php file that you want to show
or you can combine both which the best solution for me:
or you can combine in a different way:
您所需要做的就是转义字符串。你已经做对了。当然,除非您要输出 HTML,否则请使用 htmlspecialchars()。
Escaping the string is all you need to do. You have done it correctly. Unless of course you are outputting for HTML, then use htmlspecialchars().
http://sandbox.phpcode.eu/g/b1316.php
http://sandbox.phpcode.eu/g/b1316.php
我假设您正在尝试显示
标签?如果是这样,只需使用
<
而不是<
I am assuming you're trying to show the
<?php ?>
tags? If so, just use<
instead of<
这不能直接按照您发布的方式完成。
我相信您知道 PHP 是一种服务器端脚本语言。这意味着它在页面显示给用户之前执行。
实际上,您的代码将文本写入浏览器,而不是服务器。执行由其他 php 代码生成的 php 代码的唯一方法是将新代码写入文件,然后在页面加载后运行该文件。
This cannot be done directly in the way that you posted.
PHP is a server side scripting language as I am sure you know. This means it is executed before the page is shown to the user.
In effect, your code is writing the text to the browser, but not to the server. The only way to execute php code generated by other php code would be to write the new code to a file, then run the file once the page loads.
示例 1
示例 2
请参阅 php 手册高亮字符串
Example 1
Example 2
See php manual highlight-string
我不确定 php 引擎需要什么(我将采用其他发帖者的话来表示 htmlentities 可以工作),但是使用 vim ?>最后会让 php 代码部分看起来已经结束。
相反,我只是这样做:
编辑很高兴。如果有更好的方法,请告诉我!
I'm unsure what the php engine needs (I'll take other poster's words for it that htmlentities works), but using vim the ?> at the end will make it look like the php code section has ended.
Instead I just do this:
Editor is happy. If there is a better way of doing this, let me know!
作为这里许多答案的总结,您可以通过将所有特殊字符更改为其相应的 html 特殊代码来
echo
oprint
php 和 html 代码。至少字符串中的所有“<”字符都应转换为“<”。有很多方法可以做到这一点:这会转换回显代码所需的所有字符。
这与
htmlspecialchars
的作用相同,但也会转换具有相应 html 代码的所有字符。值得注意的是,单引号和双引号具有不同的行为,因为单引号不会解释其中的变量,也不会读取转义字符(除了单引号本身)。
您也可以使用 heredoc 语法,它与
双引号
字符串相同,但不需要在字符串内转义它们或 nowdoc > 语法相同,但模仿单引号字符串
您可以在此处阅读有关这些内容的所有内容:
https://www.php.net/manual/es/language。类型.string.php
As a summary of many answers here you can
echo
oprint
php and html code by changing all special characters to its correspondent html special codes. Al least all '<' chars in string should be converted to '<'. There are many ways of doing this:This converts all characters needed for echoing code.
This does the same as
htmlspecialchars
but also converts all characters that have corresponding html codes.Something worth noting is that
single
anddouble quotes
have a different behavior assingle quotes
do not interpret variables inside them nor read escaped characters as such (except the single quote itself).Also you could use heredoc syntax, that is equal as a
double-quoted
string but you don't need to escape them inside the stringOr nowdoc syntax wich is the same but imitates a
single-quoted
stringYou can read all about these here:
https://www.php.net/manual/es/language.types.string.php
在您的情况下,您应该使用 htmlspecialchars 而不是 htmlentities - htmlspecialchars 只是更轻 - htmlspecialchars 只会将特殊字符(可能会导致 HTML 呈现问题)转换为其等效的 HTML 实体,例如 >、<、单个/双引号。
htmlspecialchars 将转换具有等效 HTML 实体的所有字符,例如 á、é 等。
两者都可以,而懒惰(非高效)的决定是始终使用 htmlentities,而不考虑代码的上下文。
In your case, you should user htmlspecialchars instead of htmlentities - htmlspecialchars is just lighter - htmlspecialchars will only convert special characters (that may cause problems in the rendering of the HTML) to their HTML entity equivalent, such as >, <, single/double quotes.
htmlspecialchars will convert all characters that have an HTML entity equivalent, such as á, é, etc...
Both will work, and the lazy (non-efficient) decision is to just always use htmlentities without considering the context of the code.