Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

PHP Build a Simple PHP Application Integrating with PayPal Redirecting Invalid Shirt IDs

How doest $_GET"id" variable know that "id" is a "product_id"?

I the past tutorials we made the keys from arrays loading into working variable called $product_id. When the $GET variable sends request for id to the browser, does it literally scan the browser entry for part of the text, that says "id="? I hope my question makes sense.

Cheers Marcin

5 Answers

$_GET is a PHP super global which is parsing Browser URL and scanning&retrieving given parameter by name.

For example if your URL is like: http://example.com?my_param=somevalue

Using PHP and $_GET, it is possible to fetch that value like this:

<?php
$myParam = $_GET['my_param'];
?>

Here is some more information about $_GET: http://php.net/manual/en/reserved.variables.get.php

Thanks Ayan. Nice one.

Marcin

Do you mean this?:

<?php
$product_id = $_GET['id'];
?>

Yes. Now with regards to your code snippet - How does GET request know, what value to put between square brackets? I think, it literally scans the browser entry for piece of text like this. blah blah blah - ohh... there it is "id=...". Match-Hurray!! I just would like someone to confirm, my understanding is ok.

Cheers Marcin

Aaron Munoz
Aaron Munoz
11,177 Points

Just to explain this to others in the context of the course.

Earlier in this course you programmed your shirts.php file to set a GET variable in the url (around line 17 or so)

echo '<a href="shirt.php?id=' . $product_id . '">';

When you put a question mark(?) after the url, you can set a variable and assign(=) it a value.

That $product_id variable comes from your array in your products.php file.

If you look at your url every time you click on a different shirt, the id(or whatever you want to call the variable) changes to it's corresponding id set in the array. PHP knows the $_GET action is to look for that variable.

So, in a nutshell this is what you did:

set key in the $products array>>>assigned the key as a value to the variable $product_id>>> assigned the $product_id as a value to the GET variable in your url>>>

And that's how you make dynamic pages work! Hope that made sense!

You must know with key is in the url query string. If you want to use another key you have to update your script.

http://example.com/?page=edit&id=5&array[]=a&array[]=b

<?php
$page = $_GET['page']; // edit
$id = $_GET['id']; // 5
$array = $_GET['array'];
echo $array[0]; // a
echo $array[1]; // b
?>

Hi Nick Thanks for immediate answers. Commencing - PHP has to know, which part of the url query string is a key, loaded previously into browser from the working $product_id variable?

Cheers M