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 Build a Basic PHP Website Listing and Sorting Inventory Items Displaying All Items

Generating html using PHP-Why single the quote comes first before the double quote also we concatenate inside the quotes

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

This could also be written like the following:

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

However, this does require using double quotes. To answer your question more clearly, your example doesn't require the use of double quotes first and single quotes second (vice versa), only in the example above requires double quotes. I believe it's just a coding standard to do it that way and when you use single quotes in HTML, you don't have to escape the string.

1 Answer

Often there is more than one correct way to write a line of code. Use what is easiest. I happen to agree and prefer combining "double" and 'single' quotes to avoid concatenating. It feels more natural when reading. Here's a third approach.

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

It lines up nicer in my code editor than when using markdown.

I believe this is an older course with more introductory concepts. Perhaps this is part of the reason? I noticed a few shorthands that weren't accepted for the earlier code challenges.

Things like the ternary operator and the short echo tag which is really neat and easy to read.

// Example
<p><?= date("Y") ?> Personal Media Library</p>