php 你能用echo把PHP放在PHP里面吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15228026/
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 08:50:30  来源:igfitidea点击:

Can you put PHP inside PHP with echo?

phphtml

提问by user1729506

In a situation where little snippets of PHP are used in the html a lot, like Wordpress, can you use PHP inside PHP echos?

在 html 中大量使用 PHP 的小片段的情况下,例如 Wordpress,您可以在 PHP 回声中使用 PHP 吗?

Example:

例子:

<?php 
    echo "<?php the_author_meta('description'); ?>";
?>

As unnecessary as that may be, can it even be done? If not, one aspect of PHP that still seems to confuse me slightly is how to end and restart PHP when outputting HTML.

尽管这可能是不必要的,但它甚至可以做到吗?如果没有,PHP 的一个方面似乎仍然让我有点困惑,那就是在输出 HTML 时如何结束和重新启动 PHP。

Case in point, Chris' answer here: How can I echo HTML in PHP?- I want so badly to put a ?>at the end of his example, but that causes errors. Can someone point me in the direction of some comprehensive info of how this starting/stopping with PHP works when blending with HTML, HTML that itself may use PHP snippets in it.

举个例子,Chris 的回答是:How can I echo HTML in PHP? - 我非常想?>在他的例子的末尾放一个,但这会导致错误。有人可以向我指出一些全面的信息,说明在与 HTML 混合时,使用 PHP 启动/停止是如何工作的,HTML 本身可能会在其中使用 PHP 片段。

采纳答案by Andrew

You cannot have PHP echo more PHP code to be evaluated because PHP interprets your code in a single pass. If you have, say, <?php echo '<?php echo "hello"; ?>'; ?>, You will literally get the text, <?php echo "hello"; ?>as output, and the interpreter will not touch it.

您不能让 PHP 回显更多要评估的 PHP 代码,因为 PHP 会在一次传递中解释您的代码。如果你有,比如说,<?php echo '<?php echo "hello"; ?>'; ?>你会从字面上得到文本,<?php echo "hello"; ?>作为输出,解释器不会接触它。

You can, however, jump in and out of PHP at will:

但是,您可以随意跳入和跳出 PHP:

<?php
echo "I am going to be interpreted by PHP.";
?>
I am not interpreted by PHP.
<?php
echo "But I am again.";
?>

If you think that you need to output PHP code that is itself re-evaluated, there is always a better way to accomplish this. If you give a specific example of what you are trying to accomplish (real-world case), then the folks here on SO would be happy to help.

如果您认为需要输出自身重新评估的 PHP 代码,那么总有更好的方法来实现这一点。如果你给出一个你想要完成的具体例子(现实世界的案例),那么 SO 上的人会很乐意提供帮助。

回答by Rohan Kumar

Try:

尝试:

<?php
    echo htmlspecialchars("<?php the_author_meta('description'); ?>");
?>

回答by mkaatman

In regards to: "one aspect of PHP that still seems to confuse me slightly is how to end and restart PHP when outputting HTML."

关于:“PHP 的一个似乎仍然让我有点困惑的方面是在输出 HTML 时如何结束和重新启动 PHP。”

<?php
// Do some wicked cool stuff in PHP here.
?>
<html>
<body>
Output some html here
<?php
//More awesome PHP stuff here.
?>
Back to html
</body>
</html>
<?php
// You can do any final stuff you want to do here.
?>

Or maybe you mean something more like this:

或者也许你的意思更像这样:

<table>
<?php 
foreach($person as $p){
  echo "<tr>";
  echo "<td>".$p['name']."</td>";
  echo "</tr>";
}
?>
</table>

回答by Niet the Dark Absol

Yes, but it's a horrible idea. In this case, you should just write the function. If you want to "link" the multiple occurrences together, create your own function that does this (such as function describe() {the_author_meta('description');})

是的,但这是一个可怕的想法。在这种情况下,您应该只编写函数。如果您想将多个事件“链接”在一起,请创建您自己的函数来执行此操作(例如function describe() {the_author_meta('description');}

In general, you need to realise that anything between <?phpand the next ?>will be considered a PHP block and parsed by the engine. Anything on in those blocks is left as is. If you're having specific issues, please ask about them specifically ;)

通常,您需要意识到介于<?php和下一个之间的任何内容都?>将被视为 PHP 块并由引擎解析。这些块中的任何内容都保持原样。如果您有具体问题,请具体询问;)