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 Simple PHP Application Listing Inventory Items Understanding Whitespace

Robert Villareal
Robert Villareal
14,566 Points

concatenation

Hello,

I'm curious on why this:

echo '<img src="' . $product["img"] . '" alt="' . $product["name"] . '">';

works, while this:

echo '<img src=" . $product['img'] . " alt=" . $product['name'] . ">';

does not. Everything is paired up nicely on the code that does not work. Why do we need the extra single quotes on the code that works?

Ben Griffith
Ben Griffith
5,808 Points

Apologies if I'm going mad, but both lines of code appear to be identical!

Robert Villareal
Robert Villareal
14,566 Points

Oops!! Sorry about that Ben. Edited my original post.

5 Answers

Andrew Shook
Andrew Shook
31,709 Points
echo '<img src="' . $product["img"] . ' " alt=" ' . $product["name"] . ' ">';
// output: <img scr="url/to/img.jpg" alt="alt text">

echo '<img src=" . $product['img'] . " alt=" . $product['name'] . ">';
// output: <img scr=url/to/img.jpg alt=alt text>

The challenge isn't passing with the second one because it is generating improper html. HTML attributes values have to be surrounded by quotes.

Are you sure you entered the code snippets correctly? On my screen, both code snippets have 6 paired up single-quote characters. I don't seen an 'extra' single quote on either.

Robert Villareal
Robert Villareal
14,566 Points

Just edited my original post. Thanks for checking it out.

You need the closing single quote on the first part of your string *'<img src=" *, The ending doublequote charaacter does not close it.

Robert Villareal
Robert Villareal
14,566 Points

Thank you all for the quick response. :D

Andrew Shook
Andrew Shook
31,709 Points

Robert, did you figure the problem out?

Robert Villareal
Robert Villareal
14,566 Points

Hi Andrew,

I did. Thanks for following up on me.