In that case, I won't give a solution in the style of the Brainf*ck language 🙂
Well,
for($i=0;$i<$n;$i++){
${"finalvar$i"} =${"x_$i"};
}
Would be equivalent to
$finalvar0 = $x_0;
$finalvar1 = $x_1;
$finalvar2 = $x_2;
...etc.
Which I understand is what you want.
Getting the values should work the same way; at least, this little bit of code works for me:
<?php
$n=20;
//Create a bunch of variables
for($i=0;$i<$n;$i++){
${"finalvar$i"} = $i; //'cos I don't have any $x_* variables
}
//Echo their values.
for($i=0; $i<$n; $i++)
{ echo ${"finalvar$i"};
}
print_r(get_defined_vars());
?>
Producing
012345678910111213141516171819
Followed by a list of all the variables I had currently defined, including
[finalvar0] => 0
[finalvar1] => 1
[finalvar2] => 2
[finalvar3] => 3
[finalvar4] => 4
[finalvar5] => 5
[finalvar6] => 6
[finalvar7] => 7
[finalvar8] => 8
[finalvar9] => 9
[finalvar10] => 10
[finalvar11] => 11
[finalvar12] => 12
[finalvar13] => 13
[finalvar14] => 14
[finalvar15] => 15
[finalvar16] => 16
[finalvar17] => 17
[finalvar18] => 18
[finalvar19] => 19
As it should.
So the last example in your list of attempts should have worked.
Is there any particular you need to have variable variables like these? I'm wondering if you wouldn't be better off using an array instead. If your $x's were in an array called $x, and you wanted to copy them all to a new array $finalvar, then you wouldn't need a loop at all; you'd just go $finalvar=$x.