Shayna23 Posted January 8, 2014 Share Posted January 8, 2014 Hi Everybody, I'm trying to simulate a "register globals = ON" environment with this: foreach($_POST as $var=>$value){ ${$var} = $value; } I just noticed that I can use the bracket-less "$$var = $value" instead of "${$var} = $value." Did I just discover something really cool and easier to type? Or are there horrible pitfalls to the bracketless $$var. I wonder which is faster? Sorry for asking such a basic (dumbo-style) question, but I couldn't google the answer because google strips out dollarsigns and brackets from my search LOL Quote Link to comment Share on other sites More sharing options...
trq Posted January 8, 2014 Share Posted January 8, 2014 I'm trying to simulate a "register globals = ON" environment Why the hell would you do that? Register globals is disabled by default for a reason. It has massive security implications. Quote Link to comment Share on other sites More sharing options...
Shayna23 Posted January 8, 2014 Author Share Posted January 8, 2014 (edited) Cute response Edited January 8, 2014 by Shayna23 Quote Link to comment Share on other sites More sharing options...
Barand Posted January 8, 2014 Share Posted January 8, 2014 "Did I just discover something really cool and easier to type? Afraid not. The manual beat you to it. http://www.php.net/manual/en/language.variables.variable.php Quote Link to comment Share on other sites More sharing options...
Shayna23 Posted January 8, 2014 Author Share Posted January 8, 2014 (edited) Oh. Edited January 8, 2014 by Shayna23 Quote Link to comment Share on other sites More sharing options...
requinix Posted January 8, 2014 Share Posted January 8, 2014 Plus variable variables won't work if you try to put arrays into your fields, such as with Quote Link to comment Share on other sites More sharing options...
objnoob Posted January 9, 2014 Share Posted January 9, 2014 Plus variable variables won't work if you try to put arrays into your fields, such as with <input type="text" name="array[]" /> Why not? Quote Link to comment Share on other sites More sharing options...
requinix Posted January 9, 2014 Share Posted January 9, 2014 Because it'll try to create a variable named "array[]". With the brackets. Quote Link to comment Share on other sites More sharing options...
objnoob Posted January 9, 2014 Share Posted January 9, 2014 Because it'll try to create a variable named "array[]". With the brackets. I doubt it. Quote Link to comment Share on other sites More sharing options...
objnoob Posted January 9, 2014 Share Posted January 9, 2014 It will create a variable name $array and the value of this variable would be an array. I'm quite certain.... Ex: <input type='text' name='array[]' value='value 1' /> <input type='text' name='array[]' value='value 2' /> <input type='text' name='array[]' value='value 3' /> On submission: <? php foreach($_POST['array'] as $value) echo $value.'<br />'; ?> value 1 value 2 value 3 On submission: <? php foreach($_POST as $var => $value) $$var = $value; if(isset($array)) foreach($array => $value) echo $value.'<br />'; ?> value 1 value 2 value 3 Quote Link to comment Share on other sites More sharing options...
requinix Posted January 9, 2014 Share Posted January 9, 2014 (edited) Eh, I tricked myself into thinking too literally. I was actually talking about $var = "array[]"; $before = get_defined_vars(); $$var = 123; $after = get_defined_vars(); print_r(array_diff($after, $before)); Array ( [array[]] => 123 ) Edited January 9, 2014 by requinix Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.