Forum Moderators: coopster

Message Too Old, No Replies

Use function's parameter as name for variable?

Will be used in a function/foreach...

         

JAB Creations

3:41 pm on May 23, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm working on a foreach loop and for it to work I need to set variables that inherit their name from the first parameter requested with the function. So...

function example($property,$value)
{
if (isset($_POST['formis']))
{
if (isset($_POST[$property])) {//$property_parameter_var_here = '123';}
}
}

function example('audio','1');

...in this example I would want the 'property_parameter_var_here' variable to actually be named '$audio'.

- John

cameraman

3:54 pm on May 23, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



function example($property,$value)
{
global $$property;
if (isset($_POST['formis']))
{
if (isset($_POST[$property])) { $$property = '123';}
}
}

JAB Creations

4:04 pm on May 23, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Ha! Very cool! What's the double dollar sign variable referred to as?

Here is a partially static echo to confirm this works...

function example($property,$value)
{
global $$property;
if (isset($_POST['formis']))
{
if (isset($_POST[$property])) {$$property = '123';}
}
echo $audio;
}

example('audio','1');

Thanks cameraman!

- John

dreamcatcher

6:24 pm on May 23, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi John,

Its a variable variable. More info here:
[uk2.php.net...]

dc