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

james tarala
james tarala
1,370 Points

For Loop and "isset"

I was wondering why the code I am trying to submit is not working? I believe the "For Loop" is accurate, even the integer association of my first "echo" portion.

Any help will be greatly appreciated!

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

?>

2 Answers

Sam Wilson
Sam Wilson
2,619 Points

Hi James,

Currently learning PHP also, I've copied your code over onto my workspace and there are a few small errors, however code as a whole is good!

Firstly, where you use the isset function it should read (isset($facts[$i])) as you are using the value of $i to point to a key within your array.

Secondly, your line breaks are the wrong way round :) , for example you have wrote " echo "$i </ br> \n" this should read "echo $i <br /> \n" to correctly create breaks and new lines.

Last off , on your final else echo line you have missed off a ';' after your echo statement, after changing these in your code it displays all good!

Also - I'm not sure if the fullstop is needed after you echo "$i" in your first if statement, I'm not sure if the challenge will see the correct output.

Correct code:

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