I need the php code in one of my design . There are two constants in a php file . One is named "con1" and the other is named "con2" . There are also two variables, "var1" and "var2" . I want to set the two variables to the two constant value . when var1 = con1 , then var2 = con2 . when var1 = con2 , then var2 = con1 . One variable is corresponding to one constant , but they are randomly correspond to one constant . Two variables can't be the same constant ! How can I achieve it with shortest php code ? Plus, do you have to define the variables before I use them ? Thanks in advance !
There are two situations . when var1 = con1 , then var2 = con2 . when var1 = con2 , then var2 = con1 . I need randomly to set a constant value to the variables . $var2 = ($var1 == con1) ? con2 : con1; Did you set var1 to con1 ? How about another situation ?
After var1 is set to con1 the ternary operator makes sure that var2 will always be the other constant. It doesn't care which constant var1 was set to as it will always choose the other one.
I need to set var1 randomly . That is to say, var1 may be set to con1 and con2 . The code you gave set var1 to con1 only . How about the other situation ?
you can use the random function, rand() to introduce the randomness $i = rand(1,2); $var1 = "con"$i; $var2 = ($var1 == con1) ? con2 : con1;
Thanks . I really appreciate you both . Actually, I take "con1" and "con2" as example. The two constant may be "constantA" and "constantB" and others . How to write the php code to achieve it ?