Appending variables to php url doesn't work.

fuzzybabybunny

Moderator<br>Digital & Video Cameras
Moderator
Jan 2, 2006
10,455
35
91

Woosta

Platinum Member
Mar 23, 2008
2,978
0
71
echo '<p>', $_GET['a'] + $_GET['b'], '</p>';

I suggest not using old/crappy references and use the link I provided in my long reply to your wanting to learn web dev ( the new version of hudzilla wiki ).
 

fuzzybabybunny

Moderator<br>Digital & Video Cameras
Moderator
Jan 2, 2006
10,455
35
91
Originally posted by: Woosta
echo '<p>', $_GET['a'] + $_GET['b'], '</p>';

I suggest not using old/crappy references and use the link I provided in my long reply to your wanting to learn web dev ( the new version of hudzilla wiki ).

Hmmm... I actually remember Nerd Paradise saying something about using $_GET['a'] (something about referring to the variable as an array rather than a single variable) literally two or three pages further in the same tutorial.

Bah, well now I know that echo $a isn't only outdated, but flat out doesn't work.
 

DaveSimmons

Elite Member
Aug 12, 2001
40,730
670
126
the echo would work -if- you assigned values to the variables $a and $b somewhere above those lines.

try
$a = 3 ; $b = 7 ;
in PHP code above that block to force values, then try changing them to $a = $_GET[ 'a' ] and $b = $_GET[ 'b' ]

 

Snapster

Diamond Member
Oct 14, 2001
3,916
0
0
Originally posted by: DaveSimmons
the echo would work -if- you assigned values to the variables $a and $b somewhere above those lines.

try
$a = 3 ; $b = 7 ;
in PHP code above that block to force values, then try changing them to $a = $_GET[ 'a' ] and $b = $_GET[ 'b' ]

 

NiKeFiDO

Diamond Member
May 21, 2004
3,901
1
76
You guys might be putting the cart before the horse.

the global $_GET array holds GET data. GET data is created within a URL.

For instance, a URL: www.mysite.com?a=10&b=20

From this URL, you can use (in php)
$a = $_GET['a'];
$b = $_GET['b'];

$a = 10 and $b= 20 - it's grabbing the values from the URL.

So, if you have a script which multiplies the two values and you want it to appear on a web page, you use some code like:
<p>This is HTML code</p>
<?php
$a = $_GET['a'];
$b = $_GET['b'];

echo "<p>";
echo $a * $b;
echo "</p>";
?>
<p>This is HTML code again</p>

This code above will take the GET values from the URL and multiply the two (and echo that value out).

If you do not have the "?a=10&b=20" part in the URL, then the $_GET['a'] and $_GET['b'] will not have any value assigned to it, and PHP will have no values to multiply together.

key/value pairs in URLs are all started with a question mark (?) and followed by key=value. Multiple name/value pairs are separated by ampersands (&)

In our example above, we see that we have two key/value pairs = a/10 and b/20 (a=10 and b=20). They are started with a "?" and separated by a "&"

Server side languages parse the URL out to these key/value pairs.

In PHP, we can grab the value of these if we know their key.

$_GET['a'] - "a" i the key which will give us the value (in our example, 10)
 

Woosta

Platinum Member
Mar 23, 2008
2,978
0
71
Originally posted by: NiKeFiDO
You guys might be putting the cart before the horse.

It's as simple as the author didn't reference the assignment of the two variables in that specific page which is why the OP didn't get it. I would assume he got it after seeing my example.

And also OP, you should be sanitizing those values regardless.

/me doesnt see why you need a page explanation of something so simple

In PHP, we can grab the value of these if we know their key.

You don't necessarily have to know the key to use values, and this isn't specific to PHP.

Server side languages parse the URL out to these key/value pairs.

If you want to be specific about it, they parse the client side HTTP requests which are sent through some type of method, such as:

GET /filename.php?foo=bar HTTP/1.1
Host: www.sitename.com

So more technically they don't parse the "URL" per say.
 

Crusty

Lifer
Sep 30, 2001
12,684
2
81
The OP was using a tutorial that was assuming register_globals was enabled. That's quite old considering PHP says
This feature has been DEPRECATED as of PHP 5.3.0 and REMOVED as of PHP 6.0.0. Relying on this feature is highly discouraged.
about register_globals.
 

Woosta

Platinum Member
Mar 23, 2008
2,978
0
71
Originally posted by: Crusty
The OP was using a tutorial that was assuming register_globals was enabled. That's quite old considering PHP says
This feature has been DEPRECATED as of PHP 5.3.0 and REMOVED as of PHP 6.0.0. Relying on this feature is highly discouraged.
about register_globals.

Yeah.. I did mention not using old/inaccurate references in my first reply.

I suggest this: http://www.tuxradar.com/practicalphp