Jump to content

Using $_GET names as a variable name


Goldeneye

Recommended Posts

What I'm trying to do is take the keyname of a $_GET variable, and create a variable-name with it.

An example:

<?php
if(isset($_GET['foobar'])){
$foobar = intval($_GET['foobar']);
}
?>

So I'm trying to take $_GET['foobar'] and extract the 'foobar' from it; append a '$' to the extracted name.

 

BUT here's the tricky part. I'm trying to generate these variables through a loop while putting all the $_GET names I want generated, into an array().

 

<?php
$URLarray = array($_GET['foo'], $_GET['bar'], $_GET['var']);
function setDefaults($URLarray){
	$x = '$';
	$URLvar = explode($URLarray, ', ');
	for($i=0; $i <= count($URLvar); $i++){
		$x.key($URLvar[$i]) = mysql_real_escape_string(intval($URLvar[$i]));
	}
}
?>

I thought it would've worked, but I get a Parse Error which tells me there's an unexpected '=' on the line with:

$x.key($URLvar[$i]) = mysql_real_escape_string(intval($URLvar[$i]));.

 

I don't have to do it this way, I could simply define every single variable I need, but there are a lot of variables and I thought this would save a kilobyte or two in filesize.

 

I'm sure I'm doing something wrong and that I don't even have to explode the $URLarray variable, but I did try that and it just returned '0'.

Link to comment
https://forums.phpfreaks.com/topic/116670-using-_get-names-as-a-variable-name/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.