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 PHP Arrays and Control Structures PHP Loops Looping with PHP

Looping with PHP

I have done it with my Code Editor and everything is working properly but here is somehow not working.

index.php
<?php

$flavors = array();
$flavors[] = array("name" => "Cookie Dough",      "in_stock" => true);
$flavors[] = array("name" => "Vanilla",           "in_stock" => false);
$flavors[] = array("name" => "Avocado Chocolate", "in_stock" => false);
$flavors[] = array("name" => "Bacon Me Crazy",    "in_stock" => true);
$flavors[] = array("name" => "Strawberry",        "in_stock" => false);


$status = 'available';

foreach ($flavors as $flavor){
    if($status === 'available' || $flavor['in_stock'] == $status)
      echo "<li>";
      echo $flavor['name'];
      echo "</li>";
}

?>

2 Answers

Jerry Gomez
Jerry Gomez
5,778 Points

I don't think you should use $status === 'available' as the previous poster mentioned. Since that will always be true.

I did it using a foreach loop, then checking the selected items 'in_stock' value as true or false. If true, then display the name of the flavor.

//add your code below this line
foreach($flavors as $oneFlavor)
{
  if($oneFlavor['in_stock'] == true)
    echo $oneFlavor["name"];
}

In the line if($status === 'available' || $flavor['in_stock'] == $status), $status === 'available' is always true since the value of $status is not changed. You could change || to && so that both conditions must be true, or change the line to if($flavor['in_stock'] == true) or if($flavor['in_stock']).

Instead of

      echo "<li>";
      echo $flavor['name'];
      echo "</li>";

try echo $flavor;. The challenge is not expecting the flavors to be list items.

To do multiple statements after an if statement, you could try surrounding the code block with { and }, such as

    if($flavor['in_stock']) {
      echo "<li>";
      echo $flavor;
      echo "</li>";
    }

but this does not pass the challenge. If you leave off the { and }, only the first line after the if statement is inside the if block, and the lines after the first line will run regardless of the truthiness of the if statement's condition.