Nick99 Posted May 24, 2008 Share Posted May 24, 2008 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 More sharing options...
Ingolme Posted May 24, 2008 Share Posted May 24, 2008 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 More sharing options...
Nick99 Posted May 24, 2008 Author Share Posted May 24, 2008 Thanks...but one more question: if I set VAR2 to be VAR1 before VAR1 is defined, will that also define VAR2 when VAR1 is defined? Link to comment Share on other sites More sharing options...
Ingolme Posted May 24, 2008 Share Posted May 24, 2008 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 More sharing options...
Nick99 Posted May 24, 2008 Author Share Posted May 24, 2008 What's the & for? Link to comment Share on other sites More sharing options...
Wander Posted May 24, 2008 Share Posted May 24, 2008 $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 More sharing options...
Nick99 Posted May 24, 2008 Author Share Posted May 24, 2008 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 More sharing options...
Wander Posted May 24, 2008 Share Posted May 24, 2008 =&, != 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 More sharing options...
Nick99 Posted May 24, 2008 Author Share Posted May 24, 2008 Thanks Wanderer that was exactly what I needed for the 34d/2nd problem, but thanks to all of you!! Link to comment Share on other sites More sharing options...
justsomeguy Posted May 27, 2008 Share Posted May 27, 2008 You can also store a variable name in a variable and access the original variable through it, like the code you posted. $var1 = 'hello';$var2 = 'var1';echo $$var2; // print 'hello' Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.