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

PHP Arrays and Control Structures > PHP Loops > For Looping

This 2nd question of this Code Challenge isn't making any sense to me. Here's my code:

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 . "\n"; 
if (isset($facts[$i])) {
    echo $ i . $facts[$i];
  }
}
Antonio Benincasa
Antonio Benincasa
5,956 Points

I've found the solution, you have to print the number when the fact is not set. So, you have to add an 'else' statement.

This is the code. You have to put this into the 'for'.

<?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];
  }else{
    echo $i . "\n"; 
  }
}

Good luck, Antonio Benincasa.

3 Answers

Antonio Benincasa
Antonio Benincasa
5,956 Points

You can't put whitespace between the $ sign and the name of the variable

The error is on line 15:

echo $i . $facts[$i];

Antonio Benincasa:

I didn't do that. The system did it when it posted my code. Even without the space between $ & i, the code doesn't work. Yes, I've tried it several times.

isset isn't deprecated in 7.1.8. 7.2.0 is in 2nd beta testing as of 8/3/2017.

Antonio Benincasa:

It works, but the odd thing is, the "$fact" value appears within the 1 - 100 numbers after 2, & not after all 100 numbers. I would have expected the desired results would have been displayed like this:

1-100 (individually printed) 2 2 is the approximate hours a day Giraffes sleep

Perhaps Question 2 on this Code Challenge should have been worded better. It's either that or I somehow didn't write the code correctly within the curly braces {} within the for loop. I stared at it so long, I couldn't see what I was doing incorrectly. Always helps to have an explanation afterward when the answer is correct.

Thanks for your help.

Antonio Benincasa
Antonio Benincasa
5,956 Points

This is not required by the exercise, but to do that you need another 'for' loop.

Like this:

<?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 . "\n";
    }
}

for($i = 1; $i <= 100; $i++){
    if (isset($facts[$i])) {
        echo $i . $facts[$i] . "\n";
    }
}

Antonio Benincasa

Antonio Benincasa:

Thanks for the added info. I wrote it down in my notes for reference.