PHP echo inside echo
Solution 1
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 HEREDOC syntax.
Solution 2
Don't do this. Multi-line echoes, especially when you've got embedded quotes, quickly become a pain. Use a HEREDOC instead.
<?php
echo <<<EOL
<h3>Hello</h3>
...
<div class"indios">
...
EOL;
and yes, the PHP inside your echo will NOT execute. 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 echo 'foo' ?>"
is NOT going to output just foo
. You'll actually get as output
<?php echo 'foo' ?>
André Ferreira
Updated on April 05, 2020Comments
-
André Ferreira about 4 years
I'm trying to call an HTML/PHP content that it's inside my database using:
<?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.
<?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.
-
Sjors Hijgenaar over 3 yearsThis is awesome! Thanks a bunch