kewley Posted December 17, 2006 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)? Link to comment Share on other sites More sharing options...
liuyang Posted December 17, 2006 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 Link to comment Share on other sites More sharing options...
bluetoother Posted December 18, 2006 Share Posted December 18, 2006 lol Link to comment Share on other sites More sharing options...
justsomeguy Posted December 18, 2006 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; Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now