你能用PHP把PHP放在PHP里吗?

时间:2021-12-14 08:09:59

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这样的html中使用PHP的小片段的情况下,你可以在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在这里回答:如何在PHP中回显HTML? - 我非常想在他的例子结尾处放一个?>,但这会导致错误。有人可以向我指出一些全面的信息,关于如何在与HTML混合时使用PHP启动/停止,HTML本身可能会使用PHP代码段。

4 个解决方案

#1


7  

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会在一次通过中解释你的代码。如果你有, '; ?>,你将从字面上得到文字, 作为输出,解释器不会触摸它。

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代码,则始终有更好的方法来实现此目的。如果你举一个你想要完成的具体例子(现实案例),那么这里的人们很乐意提供帮助。

#2


6  

Try:

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

#3


2  

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>

#4


1  

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');})

是的,但这是一个可怕的想法。在这种情况下,您应该只编写该函数。如果要将多个匹配项“链接”在一起,请创建自己的函数(例如函数describe(){the_author_meta('description');})

In general, you need to realise that anything between <?php and 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块并由引擎解析。这些块中的任何内容都保留原样。如果您遇到具体问题,请具体询问一下;)

#1


7  

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会在一次通过中解释你的代码。如果你有, '; ?>,你将从字面上得到文字, 作为输出,解释器不会触摸它。

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代码,则始终有更好的方法来实现此目的。如果你举一个你想要完成的具体例子(现实案例),那么这里的人们很乐意提供帮助。

#2


6  

Try:

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

#3


2  

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>

#4


1  

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');})

是的,但这是一个可怕的想法。在这种情况下,您应该只编写该函数。如果要将多个匹配项“链接”在一起,请创建自己的函数(例如函数describe(){the_author_meta('description');})

In general, you need to realise that anything between <?php and 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块并由引擎解析。这些块中的任何内容都保留原样。如果您遇到具体问题,请具体询问一下;)