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

PHP Build a Simple PHP Application Integrating with PayPal Forms, Arrays, and Get Variables

Jordan Chong
Jordan Chong
8,173 Points

Quiz Question followup- Why Cakebatter instead of CakebatterCookie Dough

Given the following question:

What does the following block of code output? <?php

$flavors = array("Cake Batter","Cookie Dough");

foreach ($flavors as $a => $b) {

echo $b;

exit;

}

?>

My assumption would have been that both values were stored in the $b working variable and therefore the echo command would print both flavors. Am I missing something here as to why cookie dough isn't echo'd?

2 Answers

Robert Walker
Robert Walker
17,146 Points

Hi Jordan,

You are correct, both values are stored in $b, however because you have exit; inside your foreach loop after the echo statement, only the first iteration (First Value) is echo'd.

If for example you removed the exit; you would then echo out Cake BatterCookie Dough and for $a 01.

Hope this helps.

Jordan Chong
Jordan Chong
8,173 Points

Yes! Didn't notice that, thanks!