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 Simple PHP Application Listing Inventory Items More Excitement With Arrays

kevin wagner
PLUS
kevin wagner
Courses Plus Student 21,913 Points

Trouble understanding the foreach loop in the Olde Ice Cream Shoppe Tasks

I'm having trouble using a foreach loop to load the elements from the $flavors array into an variable named $flavor. I used the following

<ul> foreach($flavors as $flavor) { <li></li> <li></li> } </ul>

I receive an error telling me I need to be using a working variable name $flavor. It is my understanding that the variable inside of the parenthesis after 'as' is the working variable. I realize that I'm not really loading anything in the above code, but if I'm supposed to load the array elements individually into a variable named $flavor as the instructions indicate, how can I do so If my working variable also needs to be $flavor? Would that not be the same as $flavor = $flavor[]? I believe my confusion is with the logic.

Kevin

5 Answers

A foreach loop's variable is only a temporary variable. The loop runs equivalent to $products' length. The variable $flavor is a temporary variable. It's value is updated every time the foreach loop kicks back to its beginning and runs; so $flavor wouldn't be $flavor[] exactly. Your logic for the foreach is fine. I assume you're not using PHP as they want you to in the challenge, with the tricky opening and closing loop braces. You want the loop to exist with the list items inside of it, so that they update each time through with the current item in the array, being equivalent to the counter of the loop.

  <?php foreach($flavors as $flavor){ ?>
    <li><?php echo $flavor1; ?></li>
    <li><?php echo $flavor2; ?></li>
  <?php } ?> 

The code won't work the variables $flavor1 and $flavor2 are not set. You should only echo $flavor.

It will work in this case.. The following question is where you update the code to print out the current flavor. On this particular question they only want you to set it up. I'm aware that $flavor 1, and $flavor 2 has no value or reference to $flavor.

Aha. I'm sorry, didn't know that...

Julian Price
Julian Price
11,760 Points

I think I had similar issue in that series until I moved in to further in series that made Sense to challenge

The foreach loop will loop through each position in a array and the working variable will be set as the value in current position in the array.

A practical example:

$fruitsArray = ["Banana", "Apple", "Pear"];

foreach ($fruitsArray as $fruit) {

    echo $fruit . "\n";

}

This code will display:

Banana

Apple

Pear

This is what "happens" (except that the $fruit variable is local which means it is only available in the loop):

$fruitsArray = ["Banana", "Apple", "Pear"];

$fruit = $fruitsArray[0]; // value is "Banana" in array position 0

echo $fruit . "\n";

$fruit = $fruitsArray[1]; // value is "Apple" in array position 1

echo $fruit . "\n";

$fruit = $fruitsArray[2]; // value is "Pear" in array position 2

echo $fruit . "\n";

Remaking the foreach loop to a regular for-loop:

$fruitsArray = ["Banana", "Apple", "Pear"];

for ($i = 0; i < sizeof($fruitsArray); $i++) {

  $fruit = $fruitsArray[$i];

  echo $fruit . "\n";

}

Hope this helps you understand the logic!

kevin wagner
PLUS
kevin wagner
Courses Plus Student 21,913 Points

This worked! You're right, the placement of the curly braces was throwing me off. Adding to that confusion as well were the two list item variables that had not been declared, but now I understand that this challenge allows for the solution to be applied in small steps. Thanks for the help!

Hi Kevin,

Would you mind clicking the 'best answer' link on the answer that helped you?