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

Nancy Melucci
PLUS
Nancy Melucci
Courses Plus Student 36,143 Points

PHP Arrays ForLoop isset() function

The first task of the challenge is super easy (make the for-loop from 1 to 100) but I cannot figure out the second task, and when it fails all it does is tell me Task 1 no longer passes. No helpful feedback of any kind, which makes me feel like I am stuck in a futile infinite loop. The point of the exercise is to get the for loop to detect when the key from $facts is attached to a value that can be printed. That's when it all goes to heck in a handbasket. I guess I don't understand how to use isset with the array and for loop. Please help. Thanks.

<?php
$facts = array(
    57 => ' on Heinz ketchup bottles represents the number of varieties of pickles the company once had.',
    2 => ' is the approximate hours a day Giraffes sleeps',
    18 => ' is the average hours a Python sleeps per day',
    10 => ' per cent of the world is left-handed.',
    11 => ' Empire State Buildings, stacked one on top of the other, would be required to measure the Gulf of Mexico at its deepest point.',
    98 => '% of the atoms in your body are replaced every year',
    69 => ' is the largest number of recorded children born to one woman',
);
//add your loop below this line
for ($x = 1; $x <= 100; $x++) {
  echo $x;
if (isset($facts[$x])) {
  echo($facts['value'];
}

?>

3 Answers

Matthew Carson
Matthew Carson
5,965 Points

Okay so apparently the code challenge does want you to print out all of the numbers.

Task one is printing out all of the numbers. So move the echo $x; back outside the conditional where you originally had it and it should fix that part.

Something like this:

<?php
for ($x = 1; $x <= 100; $x++) 
{
    echo $x;
    if (isset($facts[$x])) 
    {
        echo($facts[$x]);
    }
}
Matthew Carson
Matthew Carson
5,965 Points

Okay a couple things:

In order to access the fact you would use its key.

<?php
echo $facts[$x];

When $x is set to 2 it would check for $facts[2]. Which would return 'is the approximate hours a day Giraffes sleeps'. It's pretty much like saying get me the value where the key is 2.

Second, you should probably only echo $x if the array has a key of $x. Otherwise for each iteration before even checking if a fact exists it would echo out that number.

Like this:

192021222324252627282930313233343536373839404142434445464748495051525354555657 on Heinz ketchup bottles represents the number of varieties of pickles the company once had.

You probably don't want to see 19 20 21 22 etc... You probably want something more like:

57 on Heinz ketchup bottles represents the number of varieties of pickles the company once had.
<?php
for ($x = 1; $x <= 100; $x++) 
{
    if (isset($facts[$x])) 
    {
        echo $x;
        echo($facts[$x]);
    }
}
Nancy Melucci
Nancy Melucci
Courses Plus Student 36,143 Points

I like the solution and it works....the statements print out. But it still doesn't pass and it still only gives me the "task 1 is no longer passing" admonishment without any other information.

Starting to think there is something wrong with it.

: (