Home > Blockchain >  php loop executing only once echo statement for img tag
php loop executing only once echo statement for img tag

Time:09-27

I was trying to print multiple images using img tag in HTML using PHP loops, but it was only printing it once. here is what I tried :

  $rating = 5;
    for ($i = 1; $i <= 5; $i  ) {
      echo($i);
      echo("<img style='width: 20px; height:20px;' src='../project/images/star.png'");
    }
  ?>

Output: image

If I remove the img tag it works as expected.

CodePudding user response:

You don't have a closing angle bracket for the img tag.

This should work

  $rating = 5;
    for ($i = 1; $i <= 5; $i  ) {
      echo($i);
      echo("<img style='width: 20px; height:20px;' src='../project/images/star.png' alt='an image of a star' />");
    }
  ?>

A good way to troubleshoot something like this in the future is to view the source code of the browser page after it has loaded (Ctrl U for Chrome on Windows)

That way you will see how the actual HTML code looks

It's also a good idea to add an alt tag, which is what would display if the image couldn't be loaded, or by a screen reader for someone with visual impairment.

  • Related