Smudger Smudger - 5 months ago 37
PHP Question

PHP Variables - Concatenate variable name

I have a PHP for loop:

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

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


what I am trying to acheive 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

$check1="some value"

$check2="some value"

$check3="some value"

$check4="some value"

etc etc

How can I acheive this? am I missing something obvious?

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

any advice?

Thans as always,

Answer Source

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;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download