tags:

views:

143

answers:

2

How to create dynamic incrementing variable using "for" loop in php? like wise: $track_1,$track_2,$track_3,$track_4..... so on....

+3  A: 

Use parse_str() or ${'track_' . $i} = 'val';.

Alix Axel
Why the downvote?
Alix Axel
+1 for ${...} instead of variable variables.
Felix Kling
${...} is still a variable variable.
Lotus Notes
Y variable variables should be avoided?
OM The Eternity
What If I need the Value of variable previuos than the current variable? That is, ${'track_' . $i-1} can I do this?
OM The Eternity
Why do you want to do that? Likely you should use an array.
Lo'oris
@Parth: You can but you should use it like this: `${'track_' . ($i - 1)}` to keep the concatenation and the arithmetic clearly separated.
Alix Axel
+3  A: 
<?
for($i = 0; $i < 10; $i++) {
  $name = "track_$i";
  $$name = 'hello';
}

print("==" . $track_3);
Palantir