kewley 0 Posted December 17, 2006 Report Share Posted December 17, 2006 Lets say I have two variables: $var1 = 4;$var2 = 9; If I add them together: $var1+$var2=$var3; And output them: echo $var2; I get 49.......How would I make it actually adds the values (=13)? Quote Link to post Share on other sites
liuyang 0 Posted December 17, 2006 Report Share Posted December 17, 2006 <?php$var1 = 4;$var2 = 9;$var1+$var2=$var3; // $var2 = $var3 --> $var1 + $var3 --> logic wrongecho $var1; echo $var2;//no valueecho $var3;//no value$var1 = 4;$var2 = 9; $var3=$var1+$var2;echo $var3; // value is 13 Quote Link to post Share on other sites
bluetoother 0 Posted December 18, 2006 Report Share Posted December 18, 2006 lol Quote Link to post Share on other sites
justsomeguy 1,135 Posted December 18, 2006 Report Share Posted December 18, 2006 What liuyang was showing is that you have the order wrong. The equals (assigment) operator is a right-to-left operator. So, everything on the right side of the operator gets evaluated, and stored to whatever is on the left side. So, with this statement:$var = 1 + 2 + (3 * 4 * (5 / 6) * 7) + 8;Everything on the right side of the equals (the equation) gets evaluated first, and then assigned to what is on the left side of the equals (the variable). So, if you want to add $var1 and $var2 and store the result in $var3, it is written like this:$var3 = $var1 + $var2; Quote Link to post Share on other sites
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.