23

I have a PHP for loop:

for ($counter=0,$counter<=67,$counter++){

echo $counter;
$check="some value";

}

What I am trying to achieve is use the for loop variable and append it to the name of another variable.

Bascially, I want the PHP output to be as follows for each row

1
$check1="some value"

2
$check2="some value"

3
$check3="some value"

4
$check4="some value"

etc etc 

I have tried $check.$counter="some value" but this fails.

How can I achieve this? Am I missing something obvious?

Dharman
  • 30,962
  • 25
  • 85
  • 135
Smudger
  • 10,451
  • 29
  • 104
  • 179
  • Would the use of an `array` be preferable to this method? `$myVar = array(); for($i = 0; $i <= 67; $i++) { $myVar[] = "Some Value"; }` – Matt Jul 31 '12 at 12:40

4 Answers4

75

The proper syntax for variable variables is:

${"check" . $counter} = "some value";

However, I highly discourage this. What you're trying to accomplish can most likely be solved more elegantly by using arrays. Example usage:

// Setting values
$check = array();
for ($counter = 0; $counter <= 67; $counter++){
    echo $counter;
    $check[] = "some value";
}

// Iterating through the values
foreach($check as $value) {
    echo $value;
}
  • Thanks Tim, is there a situaton when this is acceptable or is it functionality no longer used? – Smudger Jul 31 '12 at 12:50
  • 1
    @Smudger: Off the top of my head, I can't think of a solid case where one would use variable variables. Looking at the [comments on the documentation page](http://php.net/manual/en/language.variables.variable.php#usernotes) might give you some ideas on where they could be useful. Just keep in mind that what you read there may not be considered best practice. –  Jul 31 '12 at 12:55
  • Concatenation is never the "cleaner" solution, but there may be cases where you have old, highly developed code that was never intended to correlate A+B where it is simply much easier to do this than rewriting hundreds of lines of code just to replace old variable types with arrays – Nosajimiki Jul 13 '17 at 21:46
  • The one case I can think of where I use them is with HTML forms where I have recursively built inputs. When you Post it, it will post as a bunch of variables and not as an array; so, recursively calling them may be easier with $_POST[${"myInputName".$i}] – Nosajimiki Jul 13 '17 at 22:00
6

You should use ${'varname'} syntax:

for ($counter=0,$counter<=67,$counter++){
    echo $counter;
    ${'check' . $counter} ="some value";
}

this will work, but why not just use an array?

$check[$counter] = "some value";
Adunahay
  • 1,551
  • 1
  • 13
  • 20
6

This is usable in some cases. For example if your app has something like 2 language entries in DB.

echo $this->{'article_title_'.$language};

That's much more usable than for example this;

if($language == 'mylanguage1')
    echo $this->article_title_mylanguage1;
else
    echo $this->article_title_mylanguage2;

Obviously this is what you should not have to do in your multilingual app, but i have seen cases like this.

arma
  • 4,084
  • 10
  • 48
  • 63
  • Yeah, it is my case: I got 3 columns entries in DB. So i need to loop like that from a checkboxes group – Pathros Sep 22 '18 at 12:46
0

An array would accomplish this.

$check = array();

for($counter = 0; $counter <= 67; $counter++) {
    $check[] = "some value";
    var_dump($check[$counter]);
}
Matt
  • 6,993
  • 4
  • 29
  • 50