Jump to content

Making variable names Dynamic


Nick99

Recommended Posts

Hey would I be able to make a variable name Dynamic? Maybe I can show you through an example

$variable = 12;if ($variable == 12){//make variable name to "VARIABLE2"}print("$VARIABALE2");

Any help would be GREATLT appreciated

Link to comment
Share on other sites

Umm... well, wouldn't you just assign $variable to $VARIABLE2?$variable = 12;if ($variable == 12){$VARIABLE2 = $variable;}print($VARIABLE2);

Link to comment
Share on other sites

I'm afraid not. I can't think of how to make a variable dependent on another at the moment. I don't know if it's possible but it might be.Edit: I just did a bit of research. I haven't tested this and I'm not sure if it will work, but I found this:$obj2 = &$obj1;

Link to comment
Share on other sites

$var1 =& $var2sort of connects them to each other, if u change one, the other changes too$var1 = 'test';$var2 =& $var1;$var1 = 'something';echo $var2;shows: something

Link to comment
Share on other sites

Oh so it's one of those operators like != += and -=? Thank you very much guys for your quick responses, it amazed me. Also how about appending stuff to the end of a variable name? Like var1 thend append it to be var1_01?On another forum a guy posted this code:

# $var_name = 'variable1';#  # $$var_name = 2;#  # echo $variable1;

Link to comment
Share on other sites

=&, != and += are different thingsthese: ==, ===, >, >=, >==, <, <=, <==, !=, !==are for comparing 2 thingsthese: +=, -=, *=, \=, &=, ^=, |=, %= and maybe some more i forgotare just a shorter version, $var1 += $var2 is the same as $var1 = $var1+$var2; etc.=& is something else, its just =, and a & before the variable means its connected to another variableyou can for example also use it in functions, function test(&$a){$a=123;}, then say $b=9;test($:);, now $b will be 123if you wanna have things like $var1_01, $var1_02, u can better use arrays$var1[0] = 'first';$var1[1] = 'second';$var1[] = 'third';etc.but depends for what u wanna use ityou can use $$var, that will make a new variable with the name $varfor($i=0;$i<5;$i++){ $tmp = 'var_'.$i; $$tmp = 'this is variable '.$i;}will give:$var_0 = 'this is variable 0'$var_1 = 'this is variable 1'etc..

Link to comment
Share on other sites

Archived

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

×
×
  • Create New...