Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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>