I try to print images with a for loop but the images dont load.
<?php $imagenes = array('1.png', '2.jpg', '3.png', '4.jpg','IMG_0105.JPG'); ?>
<div class="container"> <div class="row"> <div class="col-md"> <?php for ($i=0; $i < 4; $i++) { echo '<img src = "$imagenes[$i]" width = '100px' height = '100px';>'; } ?> </div> </div> </div>
The images are in the same folder as the .php file
Advertisement
Answer
Edit: Added additional solutions based on answer from @nik
This will never work because variables are not evaluated inside of single quoted strings.
Make sure that your use a string inside double quotes. Or use string concatenation to build the HTML tag. Also, you image tag has a semicolon inside of it that might break the tag.
So you can do this …
echo "<img src="{$imagenes[$i]}" width="100px" height="100px">";
or
echo "<img src="" . $imagenes[$i] . "" width="100px" height="100px">";
or
<img src="<?= $imagenes[$i]; ?>" width="100px" height="100px">
or
<img src="<?php echo $imagenes[$i]; ?>" width="100px" height="100px">