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
Kyle Germaine
8,174 Points'foreach' Question - Final Quiz - Integrating w/ Paypal
In the:
PHP Dev Track> Building Simple PHP App> Integrating w/ Paypal> 'Final Quiz'
How is the answer to the Q below: 'Cake Batter' and not 'Cake BatterCookie Dough' ?
<?php
$flavors = array("Cake Batter","Cookie Dough");
foreach ($flavors as $a => $b) {
echo $b;
exit;
}
?>
This makes no sense to me. I've spent about 3 hours of research and had to circle back through the quiz 4 times to determine this was the right answer. Still don't understand the rational. Could this be an error? Or am I just an idiot.
Thanks, Kyle
4 Answers
Ryan Stratton
19,181 PointsKyle,
I don't remember the quiz, or the code a associated with section, but the reason PHP in the posted example only outputs 'Cake Batter' is because of the keyword 'exit'. Unlike the keyword 'break', which just stops the current loop, 'exit' actually stops the execution of the entire script. So, even though you have two values in the array to be looped over, the script stops execution after echoing the first value when it sees the exit keyword.
Here's a link to the PHP documentation on the keyword - http://www.php.net/exit.
Cheers, Ryan
Kyle Germaine
8,174 PointsThanks Ryan!
I appreciate the quick response. Was pulling my hair out, literally.
Wasn't even looking at the 'exit. 'Forest through the trees', or something like that I guess.
Regards, Kyle
Ryan Stratton
19,181 PointsHappy to help. I've been in the same place many times!
Kyle Germaine
8,174 PointsHey Ryan,
Not sure if you still get updates from this post, but Ive run into a hick-up on a project of mine and if you have a minute to take a look to I would really appreciate it.
Ive got a question posted in the forum here
Thanks!
Kyle Germaine
8,174 PointsSorry, just realized I never gave you your 'best answer cred' either.
Ryan Stratton
19,181 PointsHey Kyle,
Thanks for the cred! I'll check out you new forum post and see if I can help.
Ryan