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 Wrapping Up The Project Wrapping Up The Project

Brittany Cerra
Brittany Cerra
7,424 Points

Wrapping Up The Project Final Quiz Trouble

I pasted the quiz question below. Can you explain the answer to me? How is this $b accounted for?

What does the following block of code output?

<?php

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

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

echo $a;

}

?>

2 Answers

Patrick Gerrits
Patrick Gerrits
14,614 Points

An array, as you know has key and a value.When you don't give a key in the creation of the array what are the defaults?

Then when you look at the foreach() statement. What is happening there. Every single entrey in $flavors is stored as.. $a of $b?

THink about how you would create an array when you do want to assign a key. $arrayExample = array("foo" => "bar", "bar" => "foo")

What is $a en what is $b? Now back to the question... in the example no key was given (so the system choose one) what is $a?

Brittany Cerra
Brittany Cerra
7,424 Points

Thank you! That makes sense. I wasn't considering the default keys, and that the output would, in this case, display those defaults.