Goldeneye Posted July 26, 2008 Share Posted July 26, 2008 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 More sharing options...
ratcateme Posted July 26, 2008 Share Posted July 26, 2008 try changing it to $$x.key($URLvar[$i]) = mysql_real_escape_string(intval($URLvar[$i])); Scott. Link to comment https://forums.phpfreaks.com/topic/116670-using-_get-names-as-a-variable-name/#findComment-599867 Share on other sites More sharing options...
Goldeneye Posted July 26, 2008 Author Share Posted July 26, 2008 It didn't seem to do anything; it still returns "Parse error: syntax error, unexpected '=' ..." Link to comment https://forums.phpfreaks.com/topic/116670-using-_get-names-as-a-variable-name/#findComment-599869 Share on other sites More sharing options...
DarkWater Posted July 26, 2008 Share Posted July 26, 2008 Check out the extract() function. I'd personally advise against it and just plan out which variables will definitively be sent to the page. No guesswork should be used when dealing with user input. Link to comment https://forums.phpfreaks.com/topic/116670-using-_get-names-as-a-variable-name/#findComment-599870 Share on other sites More sharing options...
Goldeneye Posted July 26, 2008 Author Share Posted July 26, 2008 I was thinking of using the extract() function; then I thought it wouldn't be a good idea in case someone discovered a way to overwrite the $_GET variables. I guess it's the same here too. Though, it'd great if there was a secure way to do this. Link to comment https://forums.phpfreaks.com/topic/116670-using-_get-names-as-a-variable-name/#findComment-599875 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.