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 trialmohammed abdi abdulahi warsame
4,926 Pointsthe veriabel $item is not defined how can they set it as src for the img
1= HOW can they make . $item["img"] as source for the image while the item is not defined variabel??! 2= what is point of the dots the surrounded on the bigening of the html . "<p>view
foreach($catalog as $item){
echo "<li><a href='#'><img src='"
. $item["img"] . "' alt='"
. $item["title"] . "' />"
. "<p>view Details</p>"
. "</a></li>";
2 Answers
Erik McClintock
45,783 PointsMohammed,
To answer your questions:
1) PHP has a loop known as "foreach" that will iterate over each item in an array or each property of an object and allow you to perform some action on them (whatever code is inside your code block, i.e. the curly braces). The syntax of a "foreach" loop in PHP is as follows:
foreach( $array_or_object_name_here as $some_temporary_variable_name_here ) {
// whatever code you want executed on each element/property goes here
}
So, if we have an array called $catalog
that contains a bunch of images, we could write our foreach
loop as follows:
foreach( $catalog as $current_image ) {
// then again, in here, i write whatever code i want to execute on each element of the $catalog array (which i will be referring to as "$current_image" based on how i wrote the foreach declaration)
}
The key thing to understand here is that you're going to be going over every item in your array when using a foreach
loop, and so in order to perform actions on the element that you're on currently, you need to have a temporary variable name declared for yourself to call. You can call it whatever you want; in the code example you provided, they chose the name "$item", in the code I just wrote, I chose the name "$current_image". That can be whatever you want, you just need something to refer to your current item as. Thus, the "$item" variable that they use IS set, and it's updated for EACH item in the array as your loop iterates over them one after the other. Thus, during the first cycle of your loop, the "$item" variable it set to equal the first element in your $catalog array, and has all of its properties available to use. Once the loop finishes with that element, it moves onto the next, and now "$item" refers to that second element in the $catalog array, and so forth until all of the elements have been handled.
2) The dots that you see are how PHP concatenates strings. In JavaScript, it's a "+" sign, which is a little more intuitive, but that's all the dots are doing. They allow you to jump in and out of strings and mash them together into one, which is what is happening in the code example.
Hope this helps to clear some things up!
Erik
mohammed abdi abdulahi warsame
4,926 Pointsthanks a lot buddy, that was more than enough, it was the best answer :) ,but i am facing more than one proplem of understanding the concept of php, even though i am at the middel of the way, but it seems like php is confusing because things goes arround all the time, i dont know how to say it but it needs more focus!!!!! and that makes it hard for the begginers like me to understand.
Julie Ragnarsson
3,320 PointsJulie Ragnarsson
3,320 PointsI love how you explained this. I also had been having trouble understanding what "$category as $item" was doing, and your explanation was clearer by far than the explanation the PHP manual.
Erik McClintock
45,783 PointsErik McClintock
45,783 PointsThat's great! I'm glad I was able to help clear some things up for you, Julie Ragnarsson!
The PHP manual (and a LOT of official documentation out there, unfortunately) is not terribly user-friendly, certainly not when you're fresh to these languages and to documentation in general. That said, you'll usually be able to find some decent, plain-English style explanations (with examples like I've provided here) if you do a search online for the concept you want to learn about and the language you're using it in, such as "foreach loop php" (as would be your search for this particular topic). Of course, posting questions on the forums here at Treehouse is also a fantastic way to learn, and you'll definitely get an answer (if not many answers) that can help you on your way! It just may not be as immediate as finding something online and pulling up a few different sites to hunt down an answer as soon as you need it :)
I have always found that it's most helpful to my particular learning style to have a given concept explained and demonstrated to me by a few different people or in a few different ways to really help ground it and give it a solid foundation in my head, so even with finding a working explanation here, it may be beneficial to hop onto Google and find some more definitions/examples, too!
Either way, again, glad I could help, and happy coding!
Erik