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

Help with task 2, using the isset function in a for loop.

Hi,

Just having a little trouble with this task, output looks somewhat correct but the challenge fails saying task one is no longer passing.

Can someone point to my error please :)

Craig

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[$i] ) ) {
    echo $i . $facts[$i];
  }
}

2 Answers

Saad Khan
Saad Khan
7,485 Points

When you put in the condition to check if $facts[$i] is set to display the number along with the information in the array, you skip the numbers 1-100 because the compiler only prints the count of $i when the $facts[$i] is true.

So you also need to add in an else statement to make sure as per the challenge that the compiler prints all the numbers even when $facts[$i] isn't true, that way you print all the numbers along with the array.

Hope this helps!

<?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;
  }
}

This works too but you can do it without an else.

Saad Khan
Saad Khan
7,485 Points

Jason Anello would love to see your take on this though I have proposed this solution keeping in mind the current skill set of the OP.

Luca Mori
Luca Mori
942 Points

To be honest, i still do not get the task, i thought i had to display the text, ONLY when a position of the array is NOT NULL.

Hi Saad,

Starting task 2 you have this:

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

Since you're already echoing the number you can put an if condition after that to optionally echo the fact.

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

There's nothing wrong with how you did it. This is just another way to look at it.

Hi Luca,

Task 2 is still expecting all the numbers.

If it helps, this is what the beginning of the output is supposed to look like:

12 is the approximate hours a day Giraffes sleeps3456789...

1 is echoed, then 2 is echoed, 2 has a fact so it's echoed, then the remaining numbers are echoed until you get to 10 which has a fact, and so on.

Saad Khan
Saad Khan
7,485 Points

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.

@Luca Mori That means if the key of the $facts array (the [$i] the number between the parenthesis is the key of the array) equals to the counter ($i that is incrementing every time it loops before it reaches 100) then also display the value of the array next to the number otherwise just print the numbers.

So basically we check if the counter equals to the key in the array by checking if $facts[i] is true or isset, because whenever it returns true it means that the incrementing counter is equal to one of the keys in the $fact array and thats when we display the text else we just display the incremental counter.

<?php
//add your loop below this line
for ( $i = 1; $i <= 100; $i++ ) {
  if ( isset( $facts[$i] ) ) { // check it true
    echo $i . $facts[$i]; // display text if true
  } else {
    echo $i; // else just the number
  }
}

Does that makes sense?

Saad Khan
Saad Khan
7,485 Points

Jason Anello Yup thats right, another way to do it but in my opinion the else statements makes it a bit more readable to the skill set of the OP.

Hi Craig,

You still want to echo out all the numbers and optionally echo out the fact if there happens to be one.