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

Can't solve the "For looping" PHP challenge.

The first step in this challenge is to display an incremental count of numbers up to 100, which I did just fine. The second part is to do a comparison of the incremental numbers against the $facts array, to see if one of the incremented numbers matches one of the keys in the array. Then I need to display the next number in the sequence i.e. if the matched key in the array is [57] then I need to display 58.

So far I've not managed to do anything other than display numbers 1 - 100 or the full array and keys. I've spent four hours on this now and am admitting defeat.

Can anyone guide me as to what I'm doing wrong?

Thanks (code is below)

<?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 ($i == isset($facts['57']['2']['18']['10']['11']['98']['69']) || $i <=100) { echo $i . "<br />\n"; } }

?>

Daniel Stopka
Daniel Stopka
13,520 Points

I have placed your code to code block, so we can have a better look at it...

<?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 ( $i == isset( $facts['57']['2']['18']['10']['11']['98']['69'] ) || $i <=100 ) { 
        echo $i . "<br />\n"; 
    } 
}
?>

1 Answer

Daniel Stopka
Daniel Stopka
13,520 Points

Hi, tough one, spent some time here also...

I'll try to explain what I did to accomplish this challenge...

First task is easy

<?php
for ( $i = 1; $i <= 100; $i++ ) {
  echo $i;
}

For second task, I'll write a task text from challenge:

Use the function isset to test if the incremented value equals one of the keys in the $facts array. If there is a key that matches, display the value after the number.

I strongly advice to open isset link and read a little bit, especially example #1 array part...

So the challenge (task 1) wants us to echo number, and then (second task) IF current number is present / ISSET in $facts array, then display the value / sentence from $facts array...

You wanted a guide, so I'm not going to send you mine result yet, but you need extra if statement inside your for loop leaving first echo where it is now... Play with it, if something write :)

Below the line

I'm not sure why, but sometimes the preview showed me a part of my code ($i = 1; $i <= 100; $i++) at the end of the preview window, even it wasn't echoed at all... maybe a small bug... I guess... tried it at local envi and it was ok...