On Jun 7, 4:40 pm, DavidSeck....@gmail.com wrote:
Hi guys,
first post :)
my question: is it possible to have dynamic variable names, I mean
something like this:
for($i=0;$i<x;$i++){
$y_$i = blabla;
}
in other words I want the number of the loop to be displayer in the
name of the variable.
Thanks for help.
Not sure why but you can do it one of two ways:
<code>
<? php
for ($i=0; $i < 10; $i++) {
if ($i == 0) {
$$i = "This is the first loop.";
$y_{$i} = "This is the first loop.";
}
if ($i == 1) {
$$i = "This is the second loop.";
$y_{$i} = "This is the second loop.";
}
if ($i == 2) {
$$i = "This is the third loop.";
$y_{$i} = "This is the third loop.";
}
if ($i == 3) {
$$i = "This is the fourth loop.";
$y_{$i} = "This is the fourth loop.";
}
if ($i == 4) {
$$i = "This is the fifth loop.";
$y_{$i} = "This is the fifth loop.";
}
if ($i == 5) {
$$i = "This is the sixth loop.";
$y_{$i} = "This is the sixth loop.";
}
if ($i == 6) {
$$i = "This is the seventh loop.";
$y_{$i} = "This is the seventh loop.";
}
if ($i == 7) {
$$i = "This is the eighth loop.";
$y_{$i} = "This is the eighth loop.";
}
if ($i == 8) {
$$i = "This is the nineth loop.";
$y_{$i} = "This is the nineth loop.";
}
if ($i == 9) {
$$i = "This is the tenth loop.";
$y_{$i} = "This is the tenth loop.";
}
}
$x = 0;
while ($x < 10) {
echo $$x . "<br />";
echo $y_{$x} . "<br />";
$x++;
}
?>
</code>