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 trialMaster Marketing
1,200 PointsHelp me understand the difference between the following codes in PHP
I can't seem to understand the following codes. I know the difference. I just don't know what the output will be. It's in the quiz and I swear I've taken it more than 10 times now. I always try to get the quizzes perfect.
<?php
$flavors = array("Cake Batter","Cookie Dough");
foreach ($flavors as $a => $b) {
echo $b;
exit;
}
?>
<?php
$flavors = array("Cake Batter","Cookie Dough");
foreach ($flavors as $a => $b) {
echo $a;
exit;
}
?>
2 Answers
Lucas Santos
19,315 PointsOk so for the first one:
<?php
$flavors = array("Cake Batter","Cookie Dough");
foreach ($flavors as $a => $b) {
//looping through values
echo $b;
exit;
}
?>
You will echo out: Cake Batter
And the second one:
<?php
$flavors = array("Cake Batter","Cookie Dough");
foreach ($flavors as $a => $b) {
//looping through keys
echo $a;
exit;
}
?>
You will echo out: 0
The reason for this is because arrays automatically store every Value along with a Key like this key => value
So the array looks like this:
$flavors = array();
$flavors[0] = "Cake Batter";
$flavors[1] = "Cookie Dough";
So 0 is the key and the value of that is Cake Batter and 1 is also a key with the value of Cookie Dough. So in the foreach loop you looped through the keys and value of the $flavors array.
The first one printed out Cake Batter because $b is the Values in the array (Cake Batter and Cookie Dough) and it said
echo $b;
exit;
So the reason why it did not print out Cookie Dough as well is because of the exit; which will cut off the loop after it loops through once which was only able to get the first value of Cake Batter.
Now for the second one it printed out 0 because it said
echo $a;
exit;
and $a in the foreach loop is representing the keys of [0] and [1]. So you looped through the keys and out putted them, but again the only reason that it only output the first key of [0] is because of the exit;
which stopped the loop from running twice and just grabbed the first key of [0] and not the second key of [1].
So basically without the exit;
it would loop through and output both keys and both values.
Just remember:
foreach ($flavors as $keys => $values) {
// loop
}
Laszlo Keszericze
2,161 PointsI had the same problem... now i get it! THANKS!
Master Marketing
1,200 PointsMaster Marketing
1,200 PointsOhh right. I was wondering why it didn't print out Cookie Dough. I forgot to look at the exit() command. Thanks so much Lucas! This is really helpful! :)