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

Lucas Santos
Lucas Santos
19,315 Points

I cant pass this quiz please help

Here is the question:

I visit my flavor.php file with an extra variable in the web address, like this: http://localhost/flavor.php

If my flavor.php file has the code shown below, what does it output?

$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";

}

?>```

and the choices are: A, B, Cake Batter, Cookie Dough

Don't quite understand this part and their are about 4 or 5 questions with that same code. 

```php
if (isset($_GET["id"])) {

    if (isset($flavors[$_GET["id"]])) {

        echo $flavors[$_GET["id"]];```
Lucas Santos
Lucas Santos
19,315 Points

to my understanding if (isset($flavors[$_GET["id"]])) is checking to see if the array has valid ID's

8 Answers

I'm sorry. I'm not quite sure what you're asking. I'll try explaining again. Please ask me any follow-up questions you have.

Here's what happens:

The variables are passed through the URL after the ?. They're all put in a $_GET[] array, with the variable names and values in pairs.

We're used to seeing arrays using numbers to find elements, $flavors[1]="Cake Batter."

This array uses variable names to find elements. So $_GET["id"] will be the value associated with the variable "id."

If the URL were:

http://localhost/flavor.php?id=7&meal=dessert

then $_GET["id"] = 7

and $_GET["meal"] = "dessert"

If a particular variable isn't passed along with its value in the URL, it won't be in the $_GET[] array, and isset($_GET[]) will return false.

so isset($_GET["fruit"]) = false, because there's no variable fruit with its value passed along with the URL.

isset($_GET["meal"])= true, because there is a variable meal with its value dessert passed along with the URL.

I hope this makes sense and is helpful.

Lucas Santos
Lucas Santos
19,315 Points

Yes! ok thank you now I understand :] Just had to understand what $_GET[] was more but now I get it, thanks really appreciate the help!

We can't tell what the output would be without knowing if a) $_GET["id"] has been set and, if so, b) what its value is.

If it's not been set, you'll get A.

If it's been set and the value is 1, you'll get Cake Batter.

If it's been set and the value is 2, you'll get Cookie Dough.

If it's been set and the value is anything but 1 or 2 (like 0, 4, 100, etc.), you'll get B.

So...

http://localhost/flavor.php?id=2

would write Cookie Dough.

http://localhost/flavor.php?id=7

would write B.

(Note: The first element in an array is 0, not 1. It doesn't matter here; but it may be helpful to know this going forward.)

This code you asked about:

(isset($flavors[$_GET["id"]])

is checking to see if the array element you're trying to reference exists.

Let's say $_GET["id"]=7. Since there's no element in flavors[7], the isset test would fail. Instead of having an inappropriate value blow up your program, you do the error checking and send the program to your backup plan.

If $_GET["id"]=1, since flavors[1] exists, the program can move forward and work with the contents.

I hope this helps!

[edited to correct usage of $_GET["id"]]

Lucas Santos
Lucas Santos
19,315 Points

Understand, so what does the if (isset($_GET["id"])) do exactly. I know it checks to see if theirs an ID but where?

[I've radically edited my response. Please ignore my earlier version if you happen to see it.]

(isset($_GET["id"])) is checking to see if the _GET array variable "id" is set.

Variables passed to php (from, say, an html form) through a URL are put into a _GET array in pairs -- each with a variable name and its value. So (isset($_GET["id"])) is checking to see if there's a value set for the variable "id".

If the URL is:

http://localhost/flavor.php?id=2

then the $_GET[] variable "id" is set to 2.

If the URL is:

http://localhost/flavor.php?id=7&meal=dessert

then the $_GET[] variable "id" is set to 7 and the $_GET[] variable "meal" is set to "dessert".

Hope this helps!

If the URL is plain http://localhost/flavor.php without any variable assignments with it, then (isset($_GET["id"])) should return a false. So you'd get A as the output.

Lucas Santos
Lucas Santos
19,315 Points

I see ok so the _GET variable can be used through the URL with the ?id== part after the URL and depending what the id is in the array if the URL id matches the id in the array then the statement is true correct?

You're very welcome! I'm glad I could help.