Jump to content

Variable Variables Question


Shayna23

Recommended Posts

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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 by requinix
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.