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 Adding Available Sizes

Jason S
Jason S
16,247 Points

PHP QUIZ FORMS, ARRAYS

What does the following block of code output?

<?php

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

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

    echo $b;

    exit;

}

?>

Why was the answer cake batter? and the other one's answer with echo $a was confusing as well and i did not get the answer for that

1 Answer

Jeff Lemay
Jeff Lemay
14,268 Points

With that 'exit' call in the foreach loop, you are iterating over the elements in the array but after you echo the first element you exit the foreach loop so only 'Cake Batter' gets printed.

You can remove the 'exit' and the foreach loop will iterate through all elements in the array.

Jason S
Jason S
16,247 Points

and if it was $echo a which is the key, but there is no key ?

Kevin Korte
Kevin Korte
28,149 Points

When there is no key like this, it's going to get automatically assigned a 0 index key, so if you literally had echo $a and no exit call the output would be 01