如何在浏览器中的HTML页面上显示PHP代码?

发布于 2024-11-28 03:48:39 字数 149 浏览 0 评论 0原文

如何用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(14

栩栩如生 2024-12-05 03:48:39
<?php echo htmlentities('<?php echo \'aoeu\'; ?>'); ?>
<?php echo htmlentities('<?php echo \'aoeu\'; ?>'); ?>
夢归不見 2024-12-05 03:48:39

如果你输出一些复杂的代码,一定要使用像 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.

ˉ厌 2024-12-05 03:48:39

试试这个

<<<是赫里多克

<?php

echo <<< EOF

<?php
echo <<< EOF
EOF;
?>

EOF;

?>

Try This

<<< is Heredoc

<?php

echo <<< EOF

<?php
echo <<< EOF
EOF;
?>

EOF;

?>
风情万种。 2024-12-05 03:48:39
<?php echo '<?php echo \'aoeu\'; ?>'; ?>

如果你不转义 '<'和“>”它们将被打印为 html 标签。

<?php echo '<?php echo \'aoeu\'; ?>'; ?>

If you don't escape the '<' and '>' they will be printed as html tags.

无尽的现实 2024-12-05 03:48:39

如果您使用highligh_file,则可以使用highlight_string或highlight_file,您必须指示要显示的php文件

<?php
highlight_string('<?php echo\'hello\' ?>');
highlight_file("D:\local\ajax_bottom.php");
?>

,或者您可以将两者结合起来,这对我来说是最好的解决方案:

<?php

$string=highlight_file("D:\local\hatirlaticilar\Adminn\autocomplete\gethint.php");
highlight_string("$string");
?>

或者您可以以不同的方式组合:

<?php


highlight_string(highlight_file("D:\local\hatirlaticilar\Adminn\autocomplete\gethint.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

<?php
highlight_string('<?php echo\'hello\' ?>');
highlight_file("D:\local\ajax_bottom.php");
?>

or you can combine both which the best solution for me:

<?php

$string=highlight_file("D:\local\hatirlaticilar\Adminn\autocomplete\gethint.php");
highlight_string("$string");
?>

or you can combine in a different way:

<?php


highlight_string(highlight_file("D:\local\hatirlaticilar\Adminn\autocomplete\gethint.php"));
?>
九命猫 2024-12-05 03:48:39

您所需要做的就是转义字符串。你已经做对了。当然,除非您要输出 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().

讽刺将军 2024-12-05 03:48:39
<?php echo htmlspecialchars('<?php echo \'aoeu\'; ?>'); ?> 

http://sandbox.phpcode.eu/g/b1316.php

<?php echo htmlspecialchars('<?php echo \'aoeu\'; ?>'); ?> 

http://sandbox.phpcode.eu/g/b1316.php

走过海棠暮 2024-12-05 03:48:39

我假设您正在尝试显示 标签?如果是这样,只需使用 < 而不是 <

I am assuming you're trying to show the <?php ?> tags? If so, just use < instead of <

与君绝 2024-12-05 03:48:39

这不能直接按照您发布的方式完成。

我相信您知道 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.

我恋#小黄人 2024-12-05 03:48:39

示例 1

<?php       echo '<pre>', highlight_string('<?php
include($_SERVER["DOCUMENT_ROOT"] . "/myclass/myform.php");
?>', true), '</pre>';       ?>

示例 2
请参阅 php 手册高亮字符串

Example 1

<?php       echo '<pre>', highlight_string('<?php
include($_SERVER["DOCUMENT_ROOT"] . "/myclass/myform.php");
?>', true), '</pre>';       ?>

Example 2
See php manual highlight-string

深者入戏 2024-12-05 03:48:39

我不确定 php 引擎需要什么(我将采用其他发帖者的话来表示 htmlentities 可以工作),但是使用 vim ?>最后会让 php 代码部分看起来已经结束。

相反,我只是这样做:

string = "<? echo somefunc('strparam'); ?" . ">";

编辑很高兴。如果有更好的方法,请告诉我!

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:

string = "<? echo somefunc('strparam'); ?" . ">";

Editor is happy. If there is a better way of doing this, let me know!

不必你懂 2024-12-05 03:48:39

作为这里许多答案的总结,您可以通过将所有特殊字符更改为其相应的 html 特殊代码来 echo o print php 和 html 代码。至少字符串中的所有“<”字符都应转换为“<”。有很多方法可以做到这一点:

echo htmlspecialchars("<b>\"á'");
>> <b>"á'

这会转换回显代码所需的所有字符。

echo htmlentities("<b>\"á'");
>> <b>"á'

这与 htmlspecialchars 的作用相同,但也会转换具有相应 html 代码的所有字符。

值得注意的是,单引号和双引号具有不同的行为,因为单引号不会解释其中的变量,也不会读取转义字符(除了单引号本身)。

$foo = "value";

echo "I replace $foo with value and \n with line break";
>> I replace value with value and 
   with line break

echo 'I don\'t replace $foo with value nor \n with line break';
>> I don't replace $foo with value nor \n with line break

您也可以使用 heredoc 语法,它与双引号字符串相同,但不需要在字符串内转义它们

echo <<<EOF
Your "string' here
EOF;

nowdoc > 语法相同,但模仿单引号字符串

echo <<<'EOF'
Your "string' here
EOF;

您可以在此处阅读有关这些内容的所有内容:
https://www.php.net/manual/es/language。类型.string.php

As a summary of many answers here you can echo o print 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:

echo htmlspecialchars("<b>\"á'");
>> <b>"á'

This converts all characters needed for echoing code.

echo htmlentities("<b>\"á'");
>> <b>"á'

This does the same as htmlspecialchars but also converts all characters that have corresponding html codes.

Something worth noting is that single and double quotes have a different behavior as single quotes do not interpret variables inside them nor read escaped characters as such (except the single quote itself).

$foo = "value";

echo "I replace $foo with value and \n with line break";
>> I replace value with value and 
   with line break

echo 'I don\'t replace $foo with value nor \n with line break';
>> I don't replace $foo with value nor \n with line break

Also you could use heredoc syntax, that is equal as a double-quoted string but you don't need to escape them inside the string

echo <<<EOF
Your "string' here
EOF;

Or nowdoc syntax wich is the same but imitates a single-quoted string

echo <<<'EOF'
Your "string' here
EOF;

You can read all about these here:
https://www.php.net/manual/es/language.types.string.php

旧人 2024-12-05 03:48:39

在您的情况下,您应该使用 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.

不回头走下去 2024-12-05 03:48:39
 echo '<div class="row"><div class="col-md-6 col-md-offset-3"><div class="alert alert-success" role="alert"><p style="text-align: center;">'. php code goes here .'</p></div></div></div>';
 echo '<div class="row"><div class="col-md-6 col-md-offset-3"><div class="alert alert-success" role="alert"><p style="text-align: center;">'. php code goes here .'</p></div></div></div>';
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文