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 Including the Products Array

Stein Christensen
Stein Christensen
3,592 Points

What are Keys and Values?

foreach($array as $key => $value) { //do something }

Can someone explain what Key and Value elements are? He makes reference to them as pieces of this code.

Also, how does this code assign a value to the $_GET Varriable?

Finally, is the $_GET variable value stored in the url? (i.e. teamtreehouse.com/getvariable?id=VariableStored)

1 Answer

Casey Ydenberg
Casey Ydenberg
15,622 Points

An array is by definition a set of key-value pairs, although sometimes the keys are implicit (ie, just integers from 0 to n).

In PHP, you create an array like this:

array( 'key1' => 'value1', 'key2' => 'value2');

and access it like this:

$my_array['key1'];

The $_GET variable is a special array with a set of keys determined in the url: teamtreehouse.com/getvariable?id=VariableStored

In the PHP code serving this page,

echo $_GET['id'];

would produce

VariableStored

If you create a form with method="GET", the values in the form fields will be entered as key-values pairs in the URL (where the keys are specified by the "name" HTML attribute), and then will be accessible by the PHP code on the page accessed when the user hits "Submit". (The $_POST variable is similar but the values do not appear in the URL)