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

Bára Paulátová
Bára Paulátová
3,583 Points

how to nest conditional with isset function

Hi,

I am having trouble with the code challenge here. Could someone help me figure out how to use the isset function and how to compare the incremented numbers with the keys from the $facts array?

Thanks a lot. Bára

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 \>";
}
list($key, $val) = each($facts);
if ($i = isset($facts)) {
  echo $key . $val . "<br \>";
}


?>

Hi Bara,

I don't have access to that challenge...but I am wondering.

What are you trying to do with the for-loop? In the current context, the for-loop simply prints number 1...100 (that's all).

As for isset(), it's used to check whether the array has value inside. For example, if you use isset() with an empty array the result will be FALSE or Zero. So the result of isset() is either TRUE (1) or FALSE (0).

Now, this expression: if ($i = isset($facts)) means that assign the result of isset($facts) to $i which is not a conditional expression for an if statement.

Comparison statement uses == operator. However, it won't work with your if statement. Let's assume, for example, we change the if statement to: if ($i == isset($facts))

Now, we have $i variable with any integers from 1...100 and isset($facts) which results to a Boolean answer. So this will only be true once and that is when $i = 1 and isset($facts) = TRUE (1)

Anyway I think the "whole" statement works fine with the list() function.

while(list($key, $val) = each($facts))

See it with your full code 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++ ){
//  //echo $i . "<br>";
//}

while(list($key, $val) = each($facts)){

  echo $key . $val . "<br>";
}


?>

RL Happy coding!

2 Answers

Bára Paulátová
Bára Paulátová
3,583 Points

Hi Remylus,

thanks for your answer.

1) exactly - the for loop is supposed to just show number 1-100 - that was first part of the challenge and went through ok 2) the second part of the challenge (the one I have troubles with) is this:

Within the FOR loop, after displaying the number, add a conditional that uses 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. Note: all numbers between 1 and 100 should still be displayed

I see the mistake I made with the operator ( = vs. ==) .. however, still lost how to connect the Boolean answer and incremented values ($i) ..

I found this code from some other student,

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

however, even this one does not work for the challenge (it only shows the right answer if I try it in atom and see it in browser) .. but still do not think I get the logic. Thanks for you help! b.

Bára Paulátová
Bára Paulátová
3,583 Points

Hey! I got it! :))

<?php
for ($i = 1; $i <= 100; $i++ ){
   echo $i;
   if (isset($facts[$i])) {
     echo "$facts[$i]";
   }
   echo "<br \>";
 }
?>

:) high five :) b.

Great work!