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 For Looping

Scott Murray
Scott Murray
8,353 Points

This doesn't work either.

I've tried this as well as many, many other variations, and at this point I'd really just like to see the code that passes for this task.

index.php
<?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 ($i = 1; $i <= 100; $i++) {
  echo $i . "<br />\n";
  if (isset($facts[$i])) {
  echo "$facts[$i]" . "<br />\n";  
  }

1 Answer

Jeff Wilton
Jeff Wilton
16,646 Points

I just completed this the other day, so I feel your pain. I also had it coded like you, but then changed it to this to get it to work:

for ($i = 1; $i <= 100; $i++) {
  if (isset($facts[$i])) {
    echo "$i$facts[$i]" . "<br />\n";  
  }
  else {
    echo $i . "<br />\n";
  }
}
Scott Murray
Scott Murray
8,353 Points

OMG! Are you serious!? Is there seriously any difference in how both versions of this code output. Very frustrating. Thank you though by the way. I was losing it. PS The original code I posted was missing the last bracket. I put it in and tried it and it didn't make it work. (Just in case anyone else reads this.)