PHP 回声里面的回声
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17477025/
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
PHP echo inside echo
提问by André Ferreira
I'm trying to call an HTML/PHP content that it's inside my database using:
我正在尝试使用以下命令调用它在我的数据库中的 HTML/PHP 内容:
<?php echo $row_content['conteudo']; ?>
When the row is called the HTML appears correctly but the PHP doesn't. I belieave it's cause of the echo inside the main echo.
当该行被调用时,HTML 显示正确,但 PHP 没有。我相信这是主回声内回声的原因。
<?php echo "
<h3>Hello</h3>
<?php do { ?>
<div class=\"indios\">
<a href=\"indio.php?id=<?php echo $row_indiosct['id']; ?>\">
<img src=\"galeria/indios/<?php echo $row_indiosct['foto']; ?>\" alt=\"<?php echo $row_indiosct['nome']; ?>\" />
<br /><?php echo $row_indiosct['nome']; ?></a></div>
<?php } while ($row_indiosct = mysql_fetch_assoc($indiosct)); ?> "
?>
The line one of this code is the same echo as the first code field, it's not repeating, it's there just for help and to understand where is the problem.
此代码的第一行与第一个代码字段的回显相同,它不重复,它只是为了帮助和了解问题出在哪里。
I already fixed some quotation marks but it gives an error in the line of the 1st echo.
我已经修正了一些引号,但它在第一个回声的行中给出了错误。
回答by Revent
That is some of the ugliest code I have ever seen...
那是我见过的最丑陋的代码...
<?php
echo '
<h3>Hello</h3>';
while ($row_indiosct = mysql_fetch_assoc($indiosct))
{
echo '
<div class="indios">
<a href="indio.php?id='.$row_indiosct['id'].'">
<img src="galeria/indios/'. $row_indiosct['foto'].'" alt="'.$row_indiosct['nome'].'" />
<br />'.$row_indiosct['nome'].'</a>
</div>';
}
?>
You could also use the HEREDOCsyntax.
您还可以使用HEREDOC语法。
回答by Marc B
Don't do this. Multi-line echoes, especially when you've got embedded quotes, quickly become a pain. Use a HEREDOCinstead.
不要这样做。多行回声,特别是当你有嵌入的引号时,很快就会变成一种痛苦。使用HEREDOC代替。
<?php
echo <<<EOL
<h3>Hello</h3>
...
<div class"indios">
...
EOL;
and yes, the PHP inside your echo will NOTexecute. PHP is not a "recursively executable" language. If you're outputting a string, any php code embedded in that string is not executed - it'll be treated as part of the output, e.g.
是的,您的 echo 中的 PHP 将不会执行。PHP 不是“递归可执行”语言。如果您正在输出一个字符串,则不会执行该字符串中嵌入的任何 php 代码 - 它将被视为输出的一部分,例如
echo "<?php echo 'foo' ?>"
is NOTgoing to output just foo
. You'll actually get as output
是不是要输出只foo
。你实际上会得到作为输出
<?php echo 'foo' ?>
回答by Vegard Larsen
You have misunderstood how PHP works. PHP is processed by the server. When it encounters your script, it sees the following:
您误解了 PHP 的工作原理。PHP 由服务器处理。当它遇到您的脚本时,它会看到以下内容:
<?php echo "some long piece of text that you have told PHP not to look at" ?>
What is the reasoning behind trying to nest PHP calls inside strings?
试图在字符串中嵌套 PHP 调用背后的原因是什么?
回答by Herman Andres Figueroa
evaluate code php in string using the function eval(): this post Execute PHP code in a string
使用函数 eval() 在字符串中评估代码 php:这篇文章在字符串中执行 PHP 代码
<?php
$motto = 'Hello';
$str = '<h1>Welcome</h1><?php echo $motto?><br/>';
eval("?> $str <?php ");
also if your need the code buffer output in a string also you can using the ob_start() method:
此外,如果您需要字符串中的代码缓冲区输出,您也可以使用 ob_start() 方法:
<?php ob_start(); ?>
<h3>Hello</h3>;
<?php
while ($row_indiosct = mysql_fetch_assoc($indiosct)){ ?>
<div class="indios">
<a href="indio.php?id='<?php echo $row_indiosct['id']'">
<img src="galeria/indios/'<?php echo $row_indiosct['foto'].'" alt="'.$row_indiosct['nome'].'" />
<br />'.$row_indiosct['nome'].'</a>
</div>';
<?php } ?>