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 trialMario Zulim
847 Points?1
dont know :(
<?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 = 0; ++$i <= 100;)
$facts= array ('on Heinz ketchup bottles represents the number of varieties of pickles the company once had.' => 57, 'is the approximate hours a day Giraffes sleeps' => 2,);
var_dump(isset($facts['on Heinz ketchup bottles represents the number of varieties of pickles the company once had.']));
{
echo $i ."<br /<\n";
}
2 Answers
Daniel Baker
15,369 PointsFirst step you have mostly right
<?php
//add your loop below this line
for ($i = 0; ++$i <= 100;)
{
echo $i ."<br /<\n";
}
But I would fix the html from <br /<
to <br />
Second step
Within the FOR loop, after displaying the number, add a conditional that uses the function isset (see documentation) 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
The conditional i would use in this case would be an if statement:
<?php
if () {
} else {
}
And they want you to use an isset function inside of it
<?php
if (isset()) {
// If true do this.
} else {
// If False do this.
}
I could give you the answer but I think this will get you closer. if you need more help, show me what you got and I'll be glad to continue.
Mario Zulim
847 PointsHi Daniel,
Can you explain to me how For Loops working I still did not get it correctly?
Thanks :D
Daniel Baker
15,369 PointsI will try! Did the videos not dive you a very good understanding?
This is a for loop.
<?php
for () {}
Inside the () there are three statements.
- Statement 1 is executed (one time) before the execution of the code block.
- Statement 2 defines the condition for executing the code block.
- Statement 3 is executed (every time) after the code block has been executed.
<?php
for (Statement 1; Statement 2; Statement 3) {}
- Statement 1, typically, initiates a variable. It only runs once at the very beginning of the loop.
- Think of running a race where you have to go around a track multiple times.
-
So in this case they are starting on lap 1.
<?php for ($lap = 1; Statement 2; Statement 3) {}
Statement 2, typically only checks to see if you have gone thru the loop enough times.
It will keep looping until statement 2 is a false statement.
So in this case, we only want the runner to run 5 laps. So, 1,2,3,4 and 5 are <= (less than or equal to) 5.
But the loop will stop when $lap = 6.
<?php
for ($lap = 1; $lap <= 5; Statement 3) {}
- Statement 3 is typically where you change the variable.
- But as you did in your loop, you can change the variable in other places so it is optional.
- Statement 3 will happen at the very end of the loop
- Similar to a runner passes when they pass the starting line on a race track.
<?php
for ($lap = 1; $lap <= 5; $lap++) {}
- Inside the {} is where you want your code to run.
- So if we want to display what lap the runner is on we would do this:
<?php
for ($lap = 1; $lap <= 5; $lap++) {
echo "The runner is on lap " . $lap;
echo "<br />\n";
}
/*
//This would display:
The runner is on lap 1
The runner is on lap 2
The runner is on lap 3
The runner is on lap 4
The runner is on lap 5
*/
You could add a conditional statement in the loop also:
<?php
for ($lap = 1; $lap <= 5; $lap++) {
echo "The runner is on lap " . $lap;
echo "<br />\n";
if ( $lap == 5 ) {
echo "The runner finished the race!";
echo "<br />\n";
}
}
/*
//This would display:
The runner is on lap 1
The runner is on lap 2
The runner is on lap 3
The runner is on lap 4
The runner is on lap 5
The runner finished the race!
*/
Alternatively you could prepare your statements before the loop
<?php
$laps= array(
1 => ' just started lap 1.',
3 => ' is passed the half way mark.',
4 => ' only has one lap to go!',
5 => ' has finished the race!'
);
for ($lap = 1; $lap <= 5; $lap++) {
echo "The runner" . $laps[$lap];
echo "<br />\n";
}
/*
//This would display:
The runner just started lap 1.
The runner
The runner is passed the half way mark.
The runner only has one lap to go!
The runner has finished the race!
*/
- This is because I decided not to create a $laps[2] in my array.
- But PHP continues to try it and only gave me a warning:
- PHP Notice: Undefined offset: 2 in index.php on line 10
- What I could do is check to make sure the value is set.
<?php
$laps= array(
1 => ' just started lap 1.',
3 => ' is passed the half way mark.',
4 => ' only has one lap to go!',
5 => ' has finished the race!'
);
for ($lap = 1; $lap <= 5; $lap++) {
if (isset($laps[$lap])) {
echo "The runner" . $laps[$lap];
} else {
echo "Lap " . $lap;
}
echo "<br />\n";
}
/*
//This would display:
The runner just started lap 1.
Lap 2
The runner is passed the half way mark.
The runner only has one lap to go!
The runner has finished the race!
*/
If you have more questions ask a direct one so we can get to where you are having trouble. But this explanation should give you all the tools you need for the challenge
One more just for fun:
<?php
$laps= array(
0 => 'The race hasn't started.',
1 => ' just started lap 1.',
3 => ' is passed the half way mark.',
4 => ' only has one lap to go!',
5 => ' has finished the race!',
6 => 'there is no lap 6!'
);
echo $laps[0] . "<br />\n";
for ($lap = 1; $lap <= 5; $lap++) {
if (isset($laps[$lap])) {
echo "The runner" . $laps[$lap];
} else {
echo "Lap " . $lap;
}
echo "<br />\n";
}
/*
//This would display:
The race hasn't started.
The runner just started lap 1.
Lap 2
The runner is passed the half way mark.
The runner only has one lap to go!
The runner has finished the race!
*/
Daniel Baker
15,369 PointsDaniel Baker
15,369 Pointswhat are you doing with the $facts and var_dump under the for loop?