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 Integrating with PayPal Forms, Arrays, and Get Variables

Jeff Styles
Jeff Styles
2,784 Points

Confused by quiz question - (isset($flavors [$_get["id"] ]

The question is as follows - I visit my flavor.php file with an extra variable in the web address, like this: http://localhost/flavor.php?id=1

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

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

}

?>

The line that has me quite confused seems to be

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

I understand that the $_GET id is set in the url and has a value of 1. What i don't understand is how the $flavors[$_get["id"]] is set?

I see an array with 2 index numbers, but don't see how the $_get id is correlating to those keys..

In the previous lesson we used a line of code such as $product_id = $_GET["id"];

Please help me understand this.

Thanks!

Edited to format code.

1 Answer

the $_get[id] is set by the ?1 in the browser and the $flavors[$_get["id"]] is set when the $_get[id] is = 1 as ($flavors[1] = "Cake Batter";)