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 trialDavid Choi
8,536 PointsWhat are the periods in image source for?
In the video, Alena writes the code:
echo "<li><a href='#'><img src= '"
. $item["img"] . "' alt='"
. $item["title"] . "' />"
What is the purpose of the periods in this case? Normally, they are used to join two strings together, but I'm not sure what strings are being joined together.
2 Answers
Jennifer Nordell
Treehouse TeacherHi there! They are still concatenation. At the very beginning of the video, 0:00 you can see the associative array $items.
The $item["img"]
accesses this associative array and the value held there is a string. The same goes for $img["title"]
.
The result of this will be that this line will be echoed out onto the DOM:
<li><a href='#'><img src= 'img/media/design_patterns.jpg' alt="A Design Patterns: Elements of Reusable Object-Oriented Software" />
On a side note, she goes on to close the anchor tag and make a paragraph. But yes, the dots are simply concatenation.
Hope this helps!
john paul
3,191 PointsHi Jennifer, just came across your post in the forum. I was really struggling with this problem myself, right from the start of the PHP beginners training, as there was never any explanation of why we use the period in these situations. Your explanation was perfect and now I get it !! Many thanks !!!!
John
David Choi
8,536 PointsDavid Choi
8,536 PointsThank you for your answer! However, I am still confused about what strings are being joined together. The normal example I know is:
In the code in the video, I'm not entirely too sure what strings are being joined together. I guess I'm not familiar with the periods being inside the quotations.
Jennifer Nordell
Treehouse TeacherJennifer Nordell
Treehouse TeacherHi again! Ok so let's take a closer look. In the
catalog.php
you have this:This bit of code goes through each
$catalog
in the list and turns it into an $item and prints it out as a list item. So far so good? Now let's take a look at a$catalog
. You can find all the catalog entries in thedata.php
, but let's take a look at just one of them.Now since we've set a single catalog to have a local variable name of
$item
for the entire execution of the loop we can access the associative array inside just as we would any other associative array. As you can see most of the values are strings with a few exceptions. The value for"authors"
is yet another array of strings and the value of"year"
is an integer.When we type
$item["img"]
we are accessing that array and getting the value that resides there. In this case, the value is the string "img/media/design_patterns.jpg". The same is true for $item["title"]. The value stored there is also a string of "A Design Patterns: Elements of Reusable Object-Oriented Software".I took the liberty of making an example all done in one file which might illustrate the point a little better. Please feel free to run this in a workspace and check out the results. Maybe it will make a bit more sense then.
Hope this helps clarify things!