das das - 1 month ago 20
PHP Question

PHP set a lot of variables based on increasing numbers

I need the variables u1, u2, u3, u4 ... u559 and u560 to be set to # in PHP.

I have tried:

for ($x = 0; $x <= 560; $x++) {
$u{$x} = "#"; }


But it didn't seem to work. Any help?

Answer

I think you're best off making a single string, then assigning that string as a variable name.

for ($x = 0; $x <= 560; $x++) {
    $var = 'u' . $x;
    $$var = "#";
}

However, wouldn't an array fit your purposes?

for ($x = 0; $x <= 560; $x++) {
    $u[$x] = "#";
}