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 (2018) Listing and Sorting Inventory Items Displaying All Items

What 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
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi 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! :sparkles:

Thank you for your answer! However, I am still confused about what strings are being joined together. The normal example I know is:

$test = "there!";
echo "Hello " . $test;

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
seal-mask
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi again! Ok so let's take a closer look. In the catalog.php you have this:

foreach ($catalog as $item) {
                echo "<li>" . $item . "</li>";
            }
 ?>

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 the data.php, but let's take a look at just one of them.

$catalog[101] = [
    "title" => "A Design Patterns: Elements of Reusable Object-Oriented Software",
    "img" => "img/media/design_patterns.jpg",
    "genre" => "Tech",
    "format" => "Paperback",
    "year" => 1994,
    "category" => "Books",
    "authors" => [
        "Erich Gamma",
        "Richard Helm",
        "Ralph Johnson",
        "John Vlissides"
    ],
    "publisher" => "Prentice Hall",
    "isbn" => '978-0201633610'
];

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.

<?php

$student = [
    "firstName" => "David",
    "lastName" => "Choi",
    "school" => "Treehouse",
    "course" => "PHP"
];

?>


<html>
  <head>
  </head>
  <body>
    <?php
    echo "<h1>" . $student["firstName"] . " " . $student["lastName"] . "</h1>";
    echo "<p>" . "is learning " .  $student["course"] . " at " . $student["school"] . "!!!</p>";
  ?>
  </body>
</html>

Hope this helps clarify things! :sparkles:

john paul
john paul
3,191 Points

Hi 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