你可以把PHP内的PHP与回声? [英] Can you put PHP inside PHP with echo?

查看:120
本文介绍了你可以把PHP内的PHP与回声?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在html中很少使用PHP片段的情况下,例如Wordpress,您可以在PHP回声器中使用PHP吗?


$ b 示例:

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

这可能是不必要的,它可以完成吗?如果没有,PHP的一个方面似乎仍然让我感到困惑,那就是如何在输出HTML时结束并重新启动PHP。



Chris的答案在这里:< href =https://stackoverflow.com/questions/1100354/how-can-i-echo-html-in-php>我如何在PHP中回显HTML? - 我非常想把一个?> 在他的例子结尾,但是会导致错误。有人能指出我的一些综合信息,告诉我如何用PHP开始/停止工作时,如何与HTML混合,本身可能使用PHP代码片段的HTML。

解决方案

PHP不能让更多PHP代码被评估,因为PHP一次性解释代码。如果你有<?php echo'<?php echohello; ?>; ?> ,你将会从字面上获得文本<?php echohello; ?> 作为输出,解释器不会触及它。



然而,您可以随意跳入和跳出PHP:

 <?php 
echo我将被PHP解释。;
?>
我没有被PHP解释。
<?php
echo但是我又来了。;
?>

如果您认为需要输出本身重新评估的PHP代码,更好的方法来实现这一点。如果你举了一个你想要完成的事情(真实案例)的具体例子,那么SO上的这些人很乐意提供帮助。


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

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.

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.

解决方案

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.

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

<?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内的PHP与回声?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆