Reputation: 93
I have a for
-loop ($x++) within another for
-loop ($i++) , and I want both $x AND $i to be part of a variable variable:
${'name'.$x.'place'.$i.''} = ...;
Such that I get:
However, setting variables in the way quoted above does NOT work for me (i.e. with single quotations and two variable variables). I get the error "Notice: Undefined variable [...]".
The following works:
${"name$x"} = ...;
(using double quotations and just one variable variable.)
How can I set variable variables with both $x and $i within the variable name? Thank you!
Upvotes: 0
Views: 54
Reputation: 147146
You can do this by using curly braces within your variable name assignment to separate $x
from the place
:
$x = 4;
$i = 5;
${"name{$x}place{$i}"} = "test";
echo $name4place5;
Output:
test
However it would really make a lot more sense to just use an array:
$name[$x][$i] = "test2";
echo $name[$x][$i];
Upvotes: 2