Lets say I have a variable called $file and the for loop: for($i=1; $i <= 5; $i++) {}

For each iteration of the for loop, the $i value will be appended to the $file variable name so after the for loop ends, I should have five variables: $file1, $file2, $file3, $file4, and $file5.

share|improve this question
4  
Why do you want to do this? Using an (associative) array as a portable namespace is almost always a better solution. – mu is too short Jun 4 '11 at 5:16

7 Answers

up vote 6 down vote accepted

Use ${'varname'} syntax:

for($i=1; $i <= 5; $i++) {
    ${'file' . $i} = $i;
}

However, it's often better to use arrays instead of this.

share|improve this answer
+1, forgot you can reference using the braces. – Brad Christie Jun 4 '11 at 5:18

There is a way to do this:

for($i = 1; $i <= 5; $i++) {
    ${'file'.$i} = ...;
}

But it is a bad idea to do this. Why is it a bad idea? Because this is what arrays are meant for. Do this instead:

for($i = 1; $i <= 5; $i++) {
    $file[$i] = ...;
}

(NB. It is the usual convention to start array keys at 0 rather than 1, but you do not have to do so.)

share|improve this answer
+1 Just what I was going to say. – Matty K Jun 4 '11 at 5:19

it is possible to do what you want, but creating variables on the fly seems an unusual way to solve a problem like this (i could be wrong)

I would suggest storing the filenames in an array, that way you can easily iterate over the files later on, or add an extra file and not have to change any hardcoded variable names

    $myfiles = array();

    for ($i=1; $i<=5; $i++) {
       $myfiles["file$i"] = "value set in loop";
    }

    //if you want to use the values later
    $file5_value = $myfiles["file5"];

    //if you want to loop through them all
    foreach ($myfiles as $key => $val) {
      echo "$key -> $val\n";
    }
share|improve this answer

You can use an array as well. It doesn't have the same exact affect, but it is generally what I see used in these situations.

for($i=1; $i <= 5; $i++) {
    $file[$i] = $i;
}
share|improve this answer

Try this,

 for($i=1; $i <= 5; $i++) {
       $file = $file.$i; // Concatenating vvaklue of i to file. (like file1 ,file2 etc...)   
       echo $file;
   }
share|improve this answer
I won't down vote, but this is just storing a concatenated value and not referencing a variable with a concatenated name. – Brad Christie Jun 4 '11 at 5:18

See PHP's manual on Variable Variables.

$var_name = '';
for ($i = 0; $i < 5; $i++)
{
  $var_name = 'file' . $i;

  // reference $$var_name now.
  $$var_name = 'foo';
}

var_dump($file1);
var_dump($file2);
var_dump($file3);
var_dump($file4);
var_dump($file5);

demo

share|improve this answer

See the Variable variables section in the PHP manual. You can use $$ for your needs like this:

<?php
    $var_prefix = 'file';
    for($i=1; $i <= 5; $i++) {
        $var_name = $var_prefix . $i;
        $$var_name = 'Foo Bar Blah Baz ' . $i;
    }
    var_dump($file1, $file2, $file3, $file4, $file5);
?>
share|improve this answer

Your Answer

 
or
required, but never shown
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.