¿Cómo concatenar el nombre de la variable PHP?

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 

He tratado $check.$counter="some value" pero esto falla.

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

preguntado el 31 de julio de 12 a las 13:07

Would the use of an array be preferable to this method? $myVar = array(); for($i = 0; $i <= 67; $i++) { $myVar[] = "Some Value"; } -

4 Respuestas

The proper syntax for variables variables es:

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

However, I muy desanimado this. What you're trying to accomplish can most likely be solved more elegantly by using arrays. Uso de ejemplo:

// 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;
}

Respondido el 26 de Septiembre de 17 a las 13:09

Thanks Tim, is there a situaton when this is acceptable or is it functionality no longer used? - Difuminar

@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 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. - tim cooper

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

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

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";

Respondido 31 Jul 12, 13:07

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.

Respondido 09 Jul 13, 19:07

Yeah, it is my case: I got 3 columns entries in DB. So i need to loop like that from a checkboxes group - Patros

An array would accomplish this.

$check = array();

for($counter = 0; $counter <= 67; $counter++) {
    $check[] = "some value";
    var_dump($check[$counter]);
}

Respondido 31 Jul 12, 13:07

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.