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
https://forums.phpfreaks.com/topic/285215-variable-variables-question/
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

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
)

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.