Forum Moderators: coopster

Message Too Old, No Replies

php $$ what does it mean?

         

johnblack

2:41 am on Jul 11, 2007 (gmt 0)



Hi,

I've been scratching my head over this for a while now and getting nowhere. I'm attempting to amend a piece of php code not originally written by me and to make matters worse I'm a noob at php. The code uses a variable called $actual_var but calls a function is_array() passing in $$actual_var. I don't understand what the extra dollar implies. The code looks something like this

$actual_var = $fields[$i];

if(is_array($$actual_var)) {

foreach($$actual_var as $val) {

// process $val

}
}

The extra dollar symbol may turn it into an array as the variable $$actual_var is used in a foreach statement if it is an array. But if it is an array, how is it derived from the original variable value in $actual_var which is just one element of the array $fields?

Any help greatly appreciated.

JB

Edited for typos

lavazza

2:59 am on Jul 11, 2007 (gmt 0)

10+ Year Member



[nz.php.net...]

Variable variables

...

Sometimes it is convenient to be able to have variable variable names. That is, a variable name which can be set and used dynamically...

...

A variable variable takes the value of a variable and treats that as the name of a variable. In the above example, hello, can be used as the name of a variable by using two dollar signs. i.e.

...

vincevincevince

3:07 am on Jul 11, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I believe this is code to work with register_globals on.

Sounds like the field list (i.e. all form field names) is in the $fields array, then $actual_var gets set to each field name in turn.

So, first, $actual_var might be set to $fields[0], which might be 'username'.

Then, the system checks if the form has sent an array through that field, it does this by accessing $username, which it can do by $$actual_var (because $actual_var == "username"). i.e. it using a variable to supply a variable name.

An array of checkboxes on a form, for example, will return an array in the result field. e.g. a set of checkboxes to indicate your interests on a dating site, returns all the ticked values as an array.

Make sure you get register_globals turned off, it is a very serious security hole

johnblack

3:21 am on Jul 11, 2007 (gmt 0)



Thanks for that guys, awesome replies.

lavazza

I spent ages digging around the php site and just couldn't find that page. Thanks for that.

vincevincevince

I'll check out the register_globals setting - a bit worrying to say the least!

Many thanks

JB