Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

PHP

Scott James
Scott James
4,298 Points

PHP Code Challenege on Forms, Arrays, and Get Variables

Hi

So in the last quiz there is the following question:

What does the following block of code output?

My Code

$flavors = array("Cake Batter","Cookie Dough");
     foreach($flavors as $a => $b) {
          echo $a;
          exit();
}

So the answer to this comes out as: 01, which I am taking that they are the keys for each of the items in the array?

So when it then asks to "echo $b" why is the answer only "Cake Batter" and not "CakeBatterCookie Dough"?

Thanks

8 Answers

Randy Hoyt
STAFF
Randy Hoyt
Treehouse Guest Teacher

Be sure to notice the exit command there in the middle of the foreach loop. It stops the rest of the code from processing. The first time through the foreach loop, this code will echo "0" ... and then it will stop.

Does that help?

Scott James
Scott James
4,298 Points

Hi Randy

So are you saying that the answer to the first part of the challenge should be "0"? Because the correct answer according to the challenge is "01"?

Thanks for the reply.

I got it wrong because I put 'Cake BatterCookie Dough' :P

But I guess because of the exit, it returns 0 like Randy said.

Copy and paste it into your code editor and you'll see it echo's the value 0.

Scott James
Scott James
4,298 Points

Hey Jason

Yeah 0 is what I thought should be the answer, but the only way to move forward is to select the "01" answer.

EDIT: I have just tried the quiz again, and you do have to select the "01" answer to move forward. Does that mean that the answer in that quiz is incorrect?

Thanks

Yeah your right Scott, The quiz says that 01 is the right answer. You would only get 01 if you remove the exit(); from the loop.

Scott James
Scott James
4,298 Points

Thanks for the clarification Jason. Thought I was going a bit nuts last night, trying to figure out how you get 01 as the answer!

Randy Hoyt
STAFF
Randy Hoyt
Treehouse Guest Teacher

I don't think there's a quiz question with the code posted above. The question that says echo $a doesn't have an exit. Only the question with echo $b has an exit.

Scott James
Scott James
4,298 Points

Randy, you are 100% correct! How did I miss that!! Well now I know that I have to look out for that exit command!

I have to say it is great to have the staff from Treehouse taking the time to reply to questions on the forum!

Thanks to both Randy & Jason for the replies.