Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

i got a code of 100-200 rules for making a table. but the whole time is happening the same. i got a variable $xm3, then i make a column . next row, i got $xm2 and make column. next row, i got $xm1 and make column.

so my variables are going to $xm3, $xm2, $xm1, $xm0, $xp1, $xp2, $xp3.

is there a way to make a forloop so i can fill $xm and after that a value from the for loop?

share|improve this question
    
Could you please post your code so that we can get a better idea of what you're trying to achieve. Thanks. –  Neil Aitken May 27 '10 at 15:48
3  
An array that you can access like $x['m'][2] sounds more reasonable. –  VolkerK May 27 '10 at 15:50
    
Have you considered accepting an answer for this question? –  Pez Cuckow Aug 21 '12 at 9:38
add comment

5 Answers

It is not fully clear what you are asking, but you can do

$xm = 'xm3';
$$xm // same as $xm3

in PHP, so you can loop through variables with similar names. (Which does not mean you should. Using an array is usually a superior alternative.)

share|improve this answer
add comment

In this kind of structure you'd be better off using an array for these kinds of values, but if you want to make a loop to go through them:

for($i = 0; $i <= 3; $i++) {
    $var = 'xm' . $i
    $$var; //make column stuff, first time this will be xm0, then xm1, etc.

}
share|improve this answer
add comment

As far as I am aware using different variable names is not possible.

However if you uses arrays so as below

$xm[3] = "";
$xm[2] = "";
$xm[1] = "";
$xm[0] = "";

or just $xm[] = "";

Then you can use a for each loop:

foreach($xm as $v) { echo $v; }

Edit: Just Googled and this is possible using variable names but is considered poor practice. Learn and use arrays!

share|improve this answer
1  
Variable variables (as they are known) do have some uses. but I agree that using them to emulate array like behaviour is not one of them. –  Neil Aitken May 27 '10 at 15:58
    
When would you want to use them? Just out of interest? –  Pez Cuckow May 27 '10 at 18:36
add comment

You can do this using variable variables, but usually you're better off doing this sort of thing in an array instead.

If you're positive you want to do it this way, and if 'y' is the value of your counter in the for loop:

${'xm' . $y} = $someValue;
share|improve this answer
1  
I don't think you can use variable variables in any other way than $$var. –  Tgr May 27 '10 at 15:53
1  
This more directly answers the question, but as a developer I strongly urge you opt for the array method suggested by others. –  AvatarKava May 27 '10 at 15:53
2  
@Tgr - the updated syntax I posted should work - doesn't make it a good idea, though :) –  AvatarKava May 27 '10 at 15:55
add comment

You can easily do something like this:

$base_variable = 'xm';

and then you can make a loop creating on the fly the variables; for example:

for ($i=0; $i<10; $i++)
{
  $def_variable = $base_variable . $i;
  $$def_variable = 'value'; //this is equivalent to $xm0 = 'value'
}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.