Variable Variable?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
alphaseinor
Forum Newbie
Posts: 2
Joined: Fri Oct 24, 2003 5:56 am

Variable Variable?

Post by alphaseinor »

Hello this is my first post,


Is it possible to do a variable variable?

Say I have variables:
$a1 = 1.01;
$a2 = 1.02;
$a3 = 1.04;
$total = 0.00;

and I am passing a variable from a form (using get) called:
$esc //which just happens to be 2

is it possible to do something like this:
echo $a$esc
$total = $total + $a$esc

Thanks,

Alphaseinor
User avatar
richie256
Forum Commoner
Posts: 37
Joined: Mon Oct 13, 2003 8:00 pm
Location: Montréal/Canada

Post by richie256 »

you must do this way:

Code: Select all

<?php
$a[1] = 1.01;
$a[2] = 1.02;
$a[3] = 1.04;

$esc = $_GET['esc'];

echo $a[$esc];
$total = $total + $a[$esc];
?>
alphaseinor
Forum Newbie
Posts: 2
Joined: Fri Oct 24, 2003 5:56 am

Post by alphaseinor »

Thanks

Forgot about arrays...

I think you need a raise 87)
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

You can do variable variables:
http://php.net/variables.variable
but arrays are generally easier to deal with.

Mac
Post Reply