Jump to content

Adding a # value to the end of another variable


kworden

Recommended Posts

What I'm trying to do is display a list of Values with A # value added to after the original value it represents...

See an example below:
[code]
$ga1 = 4;
$ga2 = 3;
$ga3 = 1;
$ga4 = 2;
$ga5 = 4;
$ga6 = 2; {Edited to add ';' I forgot to add when posting}

$numberofgaquestions  = 34;

for($i=1; $i<= $numberofgaquestions; $i++ ){
echo "The # $i: ". $ga$i ."<br>";
}
[/code]

The result I'm looking for is:
The # 1: 4
The # 2: 3
The # 3: 1
The # 4: 2
The # 5: 4
The # 6: 2

I get an error saying "Parse error: syntax error, unexpected T_VARIABLE, expecting ',' or ';' " So how can i add the # value to another variable?

Thanks for your help in advance....

K:)
you can  use the $$ variable variable operator:
[code]for($i=1; $i<= $numberofgaquestions; $i++ ){
    $varname = 'ga' . $i; //create name of variable as a string
    echo "The # $i: ". $$varname ."<br>"; // notice $$
}[/code]
--that will work but is not recommended, rewrite using array variables

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.