Jump to content

[SOLVED] Quick help regarding variable name


play_

Recommended Posts

I am concatenating, but not to output. I wanna concatenate to create a variable

 

here's my code

 

for($i = 0; $i <= 10; $i++) {
        // i wanna do something like this.  So it would create 10 variables. $var_0, $var_1, $var_2....etc
$var_ $i = "some string";

}
}
?>

Use:

$i = 10;

$var_10 = 'hello';

echo ${'var_' . $i};

 

EDIT Beaten to it by tippy, But anyways if you want to use variables value as part of a variables name you'll have to use variable variables, which what the above is

<?php
for ($i=1 ; $i <= 10 ; $i++)
{
    $varname = "var_$i";
    $$varname = $i;
}

echo $var_5;             // --> 5
?>

 

As said, I can't see why anyone would want to do this now the array has been invented.

<?php
for ($i=1 ; $i <= 10 ; $i++)
{
    $varname = "var_$i";
    $$varname = $i;
}

echo $var_5;             // --> 5
?>

 

As said, I can't see why anyone would want to do this now the array has been invented.

 

I already have a multidimensional array(3 arrays nesting). don't want more though.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.