0

i'd like to increment the variable within the for loop so that from "$filename" it turns into "$filename1" and then to $filename2"?

$filename = false;
$filename1 = false;
$filename2 = false;    

for ($i = null; $i <=2; $i++){
  $filename = $this->blabla;
}
2
  • 1
    Have you read about arrays yet? Commented Jul 11, 2013 at 1:36
  • yes i have, i'm just wondering if this could be done Commented Jul 11, 2013 at 1:37

2 Answers 2

2

Use variable variables:

${"filename".$i} = $this->blabla;

Documentation

Sign up to request clarification or add additional context in comments.

Comments

0

Try, something like:

for($i=0; $i<3; $i++){
  $ii = $i === 0 ? '' : $i;
  ${"filename$ii"} = $this->blabla;
}

or

for($i=0; $i<3; $i++){
  $ii = $i === 0 ? '' : $i;
  $f = "filename$ii";
  $$f = $this->blabla;
}

2 Comments

PHP doesn't use + for concatenation. But even with that fix, it's not correct.
I posted before I should have.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.