Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

Foreach question for PHP

Hey all, I'm on the building a basic PHP website tutorial and I'm having trouble deciphering some code. I understand what it's doing, but I'm having a really hard time making sense of all the double and single quotes and what they are wrapping around. Any ideas? Thanks in advance.

foreach ($catalog as $item) { echo "<li><a href='#'><img src='" . $item["img"] . "' alt='" . $item["title"] . "' />" . "<p>View Details</p>" . "</a></li>"; } ?>

1 Answer

<?php
// iterate of a  collection mumbers called $item
foreach ($catalog as $item) { 
             // for every item will print out <li> element contains <a> element
             // and <a> element contains <img> element and <p> element
             // and assigned each attribute with php $item member values.

             echo "<li><a href='#'><img src='" . $item["img"] 
                     . "' alt='" . $item["title"] . "' />" 
                     . "<p>View Details</p>" 
                      . "</a></li>"; 
  } 
?>