PHP PHP Arrays and Control Structures PHP Loops For Looping

Megan Stachnick
Megan Stachnick
1,806 Points

I'm not really understanding for loops. It said error "did you make changes to the $facts array?"

But I'm not sure what kind of changes they are looking for.

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 ($facts = 0;$array++ < 100;) {
  echo $facts . "<br />\n";
}
Bill Dowd
Bill Dowd
7,680 Points

Hello Megan, The first problem is in the "for ($facts = 0..." you overwrite the array with a zero. I'm not sure where you are in your learning, but I usually prefer a foreach loop. Here is my solution. I hope you can use it to better understand what is happening.

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

foreach ($facts as $key => $value) {
  echo $key . " = " . $value . "<br />\n";
}

As far as a for loop goes, because you have assigned a key value, you've made it hard, maybe impossible to access the key. Not a problem if it wasn't part of the sentence. However, if you try to use the following for loop, you'll get lot's of vertical space as the loop prints out blanks due to no entry in the array with that index number.

for($i = count($facts); $i >= 0; $i--) {
    echo $facts[$i] . "<br />";
}

Notice, the $i varible is set to how many elements are in the array, in this case 7. As long as the value of $i is greater or equal to 0, it will run and reduce the value of $i by 1 ($i--). Someone smarter than me might make this work, but for me, the only good way to output correctly is using a foreach loop as follows:

foreach ($facts as $key => $value) {
  echo $key . " " . $value . "<br />\n";
}
Steven Parker
Steven Parker
203,118 Points

Bill Dowd — just to avoid any confusion, your code is functional but what it does is different from the objectives specified in the instructions. So it's not an example of a solution that will pass the challenge.

1 Answer

Steven Parker
Steven Parker
203,118 Points

Initializing the loop with "$facts = 0" wipes out what was in $facts and puts a 0 in it instead. You'll probably want to use a different name for the loop variable.

You also won't need to add HTML markup for this challenge.