My personal preference is to stay away from using âechoâ if I can help it. If youâre outputting a lot of raw html, itâs a nuisance to worry about escaping quotes, which is causing some of your problems.
Hereâs a very simple PHP example. The idea is to loop over an array, and print out some html:
<?php
$arr = array("banana", "apple", "pear");
?>
<html>
<?php
foreach ($arr as $value) {
?>
<div class="fruit"><?= $value ?></div>
<?php
}
?>
</html>
If you copy and paste this code into a file called loop1.php
, and run it from the command line (php loop1.php
), youâll see this output:
<html>
<div class="fruit">banana</div>
<div class="fruit">apple</div>
<div class="fruit">pear</div>
</html>
Note there are no syntax errors, I donât have to echo anything at all, my html prints out just fine in the loop. Itâs a little bit tedious to break in and out of php, but hopefully itâs clear how itâs done. Youâre already using the shortcut <?= $blah ?>
which helps to inject a little bit of php into your html.
Hereâs the other way of doing this, which is what youâre doing: write everything as php code.
<?php
$arr = array("banana", "apple", "pear");
echo "<html>";
foreach ($arr as $value) {
echo "<div class=\"fruit\">$value</div>";
}
echo "</html>";
?>
Itâs all code, and itâs a little shorter and maybe it looks a little more clear whatâs going on. However, it requires you to write your html as a string in php. Thatâs basically what youâre doing by putting quotes around the html and echoing it. So that means you have to escape any quotes that appear in your string. âEscaping quotesâ means âtell the PHP interpreter that this quote is to be treated like part of the string, and displayed, not as code.â Otherwise, the interpreter sees the quote and thinks youâre at the end of the string being echoed and doesnât know how to proceed.
Hopefully thatâs clear.
If you want to stick with the way that youâre doing it, then hereâs a really quick fix: you will need to replace this line:
echo " <img class="img-fluid" src="images/acrilic/<?= $image ?>"> ";
with this:
echo " <img class=\"img-fluid\" src=\"images/acrilic/$image\"> ";
Notice two changes: backslash on your quotes, and I stripped out the shortcut code <?= $image ?>
, because youâre already inside a php script there. I think that should work⌠So what happens if you try that?