PDA

View Full Version : PHP question



Bok
10-01-2005, 11:02 AM
Is it possible to access the value of variable A by using the value of variable B as it's name ??

For example, let's say I have 5 variables

a_name,b_name,c_name,d_name,e_name

I have an array defined as

$myarray = array(a,b,c,d,e);

foreach ($myarray as $myvalue) {

$newname = $myvalue . "_name";

# $newname will now = a_name for the first instance

}

is it possible to get the value of a_name by referencing $newname somehow ?

I think I can do this in perl, but not sure about php ????

:cheers:

Bok

magnav0x
10-01-2005, 01:12 PM
Not sure if this is what you mean, but:




$a_name = 'test a';
$b_name = 'test b';
$c_name = 'test c';
$d_name = 'test d';
$e_name = 'test e';

$myarray = array(a,b,c,d,e);

foreach ($myarray as $myvalue) {
$newname = $myvalue . "_name";
echo $$newname."<br>"; //Will become echo $a_name, echo $b_name, etc...
}

Bok
10-01-2005, 02:21 PM
That's exactly what I needed!!!

:cheers: Mag

Bok :notworthy

magnav0x
10-01-2005, 03:06 PM
No problem, glad to help