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
Lee Anderson
7,463 PointsQuiz Forms, Arrays, And Get - I'm lost!
Hey Guys I have no Idea what the answer to this question is. I just don't understand this at all. Randy if you could help that would be awesome. What is the answer and WHY.
<?php
$flavors = array();
$flavors[1] = "Cake Batter";
$flavors[2] = "Cookie Dough";
if (isset($_GET["id"])) {
if (isset($flavors[$_GET["id"]])) {
echo $flavors[$_GET["id"]];
} else {
echo "B";
}
} else {
echo "A";
}
?>
3 Answers
Randy Hoyt
Treehouse Guest TeacherDid Michael's answer get you through it? The only thing I would add is the first part of the quiz:
I visit my flavor.php file with an extra variable in the web address, like this:
http://localhost/flavor.php?id=9
So in this quiz question, $_GET["id"] is set; it equals 9 because of the ?id=9 in the web address. But $flavors[$_GET["id"]] is not set. Since $_GET["id"] is 9, this is looking for an element # 9 in the flavors array like this: $flavors[9]. There is a $flavors[1] and a $flavors[2], but there's no $flavors[9]. Based on these two facts ...
-
$_GET["id"]is set -
$flavors[$_GET["id"]]is not set
... can you follow the conditionals and see what gets displayed?
Michael O'Malley
4,293 Points $flavors = array();
$flavors[1] = "Cake Batter";
$flavors[2] = "Cookie Dough";
if (isset($_GET["id"])) {
This first conditional checks to see if the $_GET["id"] variable is set.
if (isset($flavors[$_GET["id"]]))
This second conditional is nested within the first. Now keep in mind that this conditional ONLY runs if there is a $_GET["id"] variable.This conditional is checking to see if the "id" is within the flavors array. If the ID was 1, it would be Cake Batter which is within the array. If the ID was 2, it would be Cookie Dough which is also within the array.
echo $flavors[$_GET["id"]];
} else {
echo "B";
}
The above is still within the second conditional. So if the $_GET["id"] is either 1 (Cake Batter) or 2 (Cookie Dough), it will echo the flavor. The else in the code above will echo B if the variable is set, but not within the flavors array (again 1 or 2).
} else {
echo "A";
}
The above is the else for the first conditional. So if there is no $_GET["id"] set, the code will echo A.
Markus Nelson
Courses Plus Student 3,027 PointsIt's strange when I go to the page it does not show the extra Id that you add in the url it only shows (http://localhost/flavor.php) I was starting to loose it. - Not sure might be my browser but I'm on Chrome..
Lee Anderson
7,463 PointsLee Anderson
7,463 PointsAlright now I'm getting it! Thank you. I guess it will take some time to get everything you talk about but it is happening slowly.