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

Forms, Arrays, and Get Variables quiz problem

I'm on the last quiz in the PayPal section of the Simple PHP Application series, and this question is giving me issues:

What does the following block of code output?

<?php

$flavors = array();

$flavors[1] = "Cake Batter";

$flavors[2] = "Cookie Dough";

$x = 2;

if ($flavors[$x] != "Cookie Dough") {

echo "A";

} else {

echo "B";

}

?>

Option A: Page will display "B"

Option B: Page will display "A"

I read this to mean:

"if x doesn't equal Cookie Dough, echo A. Otherwise echo B"

and so I picked that the code would display "A" on the page, but that isn't the correct answer. I went back and looked again and my guess is that the $x variable isn't part of $flavors so the "if" statement is not true and displays the "else" value.

Can anyone clarify if this is the right explanation?

1 Answer

$flavors[2] = "Cookie Dough";
$x=2;
$flavors[$x] = "Cookie Dough";

$x is 2 and $flavors[2] is Cookie Dough. I think the NOT was throwing you off. If it is NOT Cookie Dough then it is A, but it is so it is B.

Hope that makes sense,

Ahh okay I had no idea that the value of a separate function could be applied inside another like that. I thought that when the "if" statement checked for the key it was checking for a key of "x" or a key of just "$x" (not associated with the $x variable), and not inferring to check for the value of the $x variable. Thanks for clearing that up.