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
Diego Ante
5,251 PointsForms, Arrays, and Get Variables
I was asked:
<?php
$flavors = array("Cake Batter","Cookie Dough");
foreach ($flavors as $a => $b) {
echo $a;
}
?>
And I got the answer right which is "01", but I don't understand exactly why. The other options where:
a) cake batter b)0 c)Cake batterCookie Dough
8 Answers
Diego Ante
5,251 PointsSorry I just got it. as $a stands for the key it refers that as there are 2 values in the array they are being assigned the key 0 and then 1 accordingly.
Diego Ante
5,251 PointsNow I don't understand a similar question which is:
<?php
$flavors = array("Cake Batter","Cookie Dough");
foreach ($flavors as $a => $b) {
echo $b;
}
?>
I thought the answer would be "Cake batterCookie Dough" as i thought $b as is inside a foreach would give me the value of the 2 items inside the array, but the answer was only Cake batter, why?
Jonathan Rawlins
13,998 PointsThis doesn't look like the full code for the question. I was certain their was an if statement around that question when I came to it...
Something along the lines of:
if($var < $total) {
// echo here
}
Because the use of the < (less than) is present, it will only output $b if it's less than the total. IF it was <= you would get both values.
I hope this was for the question I was thinking about.
ecp
838 PointsHi @Diego !
I'm sorry you're having trouble. I wish I could be of more help :( Would you be able to provide a link to the quiz or code challenge you're working on?
I'm reaching out to the Teaching team to see if they can help out too!
If you're still having trouble shoot me an email at help@teamtreehouse.com :)
Diego Ante
5,251 PointsHi Elizabeth,
And the question says exactly as follows:
<?php
$flavors = array("Cake Batter","Cookie Dough");
foreach ($flavors as $a => $b) {
echo $b;
exit;
}
?>
And the possible answers are:
- a) 01
- b) Cake Batter
- c) 0
- d) Cake BatterCookie Dough
The answer for the test is b) (which was Jonathan's answer and it would be correct as if he said there was a conditional, but there's none). That's why I thought the correct answer should be d) but it keeps saying it is wrong.
Randy Hoyt
Treehouse Guest TeacherHey Diego,
The exit command there makes a big difference. The exit command causes the program to stop executing immediately, without finishing all the code. (We've talked about the exit command in a couple of places when displaying values in the browser for testing.) In this case, we start in the foreach loop. We echo out the value for the first one, Cake Batter. Then, before we can loop through to the next flavor, the exit command causes the code to stop executing.
Does that help?
Diego Ante
5,251 PointsOh now i get it. Thank you very much!
Jonathan Rawlins
13,998 PointsWow, I am sorry I didnt spot that earlier Diego... I completely skimmed past the exit :(
Glad you are all set now though.