Unnamed GET vars

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Unnamed GET vars

Post by onion2k »

Is there a better way to get at unnamed variables in a GET request (eg http://www.domain.com/?1234&5678) than:

Code: Select all

$keys = array_keys($_GET);
$var1 = $keys[0];
$var2 = $keys[1];
That seems a little messy to me but I'm having a mental block..
User avatar
bokehman
Forum Regular
Posts: 509
Joined: Wed May 11, 2005 2:33 am
Location: Alicante (Spain)

Post by bokehman »

Edited: Hmm! That didn't work but this does.

Code: Select all

<?php
$i = 1; // Save variables
foreach($_GET as $k => $v){
	if(!empty($k)){
    	$var{$i} = $k;
    	$i++;
	}
} 

while($i > 0){ // Test: Print variables
	print $var{$i}."<br>\n";
	$i--;
}
?>
Last edited by bokehman on Fri Sep 02, 2005 2:24 am, edited 3 times in total.
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post by josh »

you could look at the query string / http request in the server superglobal
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

You could also strstr() it and use split() on the &. Might be easier if you have unknown amount of &-vars?
Post Reply