Jumbala102 Posted May 22, 2011 Share Posted May 22, 2011 Hi, I'm just starting PHP and I ran into a simple problem (that I can fix), but I just want to understand why my first approach doesn't work. I was testing multiplications and additions and the result I got was unexpected. Here is the code that doesn't work: <?php $number1 = 5; $number2 = 10; print $number1." multiplied by ".$number2." + 1000 = ".$number1*$number2+1000; ?> Obviously, what I want as a result when I'm displaying the page is: "5 multiplied by 10 + 1000 = 1050" What I get: "1005". That's right, nothing else. On top of getting an erroneous answer, the sentence that's supposed to display right before the equation doesn't display at all. It's even worse if I switch it around like that: print $number1." multiplied by ".$number2." + 1000 = ".1000+$number1*$number2; I get an error message and nothing displays. The obvious solution works: using a parenthesis at the beginning and at the end of the equation, which I should do regardless, but still, it's bugging me and since I just started learning PHP today, I want to understand how the code is compiled / interpreted so I don't make stupid mistakes like this one. Thanks in advance for the replies. Quote Link to comment https://forums.phpfreaks.com/topic/237088-beginner-help-adding-a-number-with-a-variable/ Share on other sites More sharing options...
r0b Posted May 22, 2011 Share Posted May 22, 2011 Something like this should work the way you want it to: <?php $number1 = 5; $number2 = 10; $result = $number1*$number2+1000; print $number1 . "multiplied by " . $number2 ."+ 1000 =" . $result; ?> I'm also a beginner and as far as I understand you have to make the dots to seperate the strings from the text properly. (example): echo $string1 . $string2 . $string3 . "text1" . "text2" . $string4 . "text3"; This probably doesn't explain the whole story, someone who knows more than me will probably explain it way better. Quote Link to comment https://forums.phpfreaks.com/topic/237088-beginner-help-adding-a-number-with-a-variable/#findComment-1218588 Share on other sites More sharing options...
requinix Posted May 22, 2011 Share Posted May 22, 2011 It's a concept called "operator precedence". In math, multiplication and division have equal precedence, but both have higher precedence than addition and subtraction. It's why 1+2*3=7 and not =9. In PHP, multiplication also has higher precedence than addition, while string concatenation is of equal precedence with addition. Adding parentheses to make it more obvious, your statement looks like print ($number1." multiplied by ".$number2." + 1000 = ".($number1*$number2)+1000); Now there's another concept: "associativity". Multiplication, addition, and concatenation all have left-to-right associativity, which means stuff gets evaluated from left to right. With more parentheses, ( +1000) ( .($number1*$number2)) ( ." + 1000 = ") ( .$number2) ( ." multiplied by ") ($number1) The last piece of the puzzle is "loose typing". PHP will convert strings to numbers and vice versa depending on what you do to them. Multiplication and addition require numbers and string concatenation requires... well, strings. After collapsing that block of stuff above to the last two steps you get "5 multiplied by 10 + 1000 = 50"+1000 As I said, addition requires numbers, so PHP converts that string into a number before using it. The result is 5. Thus print 5+1000; // 1005 Besides breaking the expression into multiple statements, you can just add parentheses. $number1." multiplied by ".$number2." + 1000 = ".($number1*$number2+1000); Quote Link to comment https://forums.phpfreaks.com/topic/237088-beginner-help-adding-a-number-with-a-variable/#findComment-1218590 Share on other sites More sharing options...
r0b Posted May 22, 2011 Share Posted May 22, 2011 I'm so glad I wasn't a smartass by trying to answer the question - just because I couldn't explain it nearly as good, or couldn't explain it at all. Great explanation requinix. Quote Link to comment https://forums.phpfreaks.com/topic/237088-beginner-help-adding-a-number-with-a-variable/#findComment-1218591 Share on other sites More sharing options...
Jumbala102 Posted May 22, 2011 Author Share Posted May 22, 2011 It's a concept called "operator precedence". In math, multiplication and division have equal precedence, but both have higher precedence than addition and subtraction. It's why 1+2*3=7 and not =9. In PHP, multiplication also has higher precedence than addition, while string concatenation is of equal precedence with addition. Adding parentheses to make it more obvious, your statement looks like print ($number1." multiplied by ".$number2." + 1000 = ".($number1*$number2)+1000); Now there's another concept: "associativity". Multiplication, addition, and concatenation all have left-to-right associativity, which means stuff gets evaluated from left to right. With more parentheses, ( +1000) ( .($number1*$number2)) ( ." + 1000 = ") ( .$number2) ( ." multiplied by ") ($number1) The last piece of the puzzle is "loose typing". PHP will convert strings to numbers and vice versa depending on what you do to them. Multiplication and addition require numbers and string concatenation requires... well, strings. After collapsing that block of stuff above to the last two steps you get "5 multiplied by 10 + 1000 = 50"+1000 As I said, addition requires numbers, so PHP converts that string into a number before using it. The result is 5. Thus print 5+1000; // 1005 Besides breaking the expression into multiple statements, you can just add parentheses. $number1." multiplied by ".$number2." + 1000 = ".($number1*$number2+1000); Wow, thank you very much for the detailed answer. It's really well explained. Just a last question, though. While I do get that concatenation and additions have the same precedence order and that the way I typed my "print" code made it so it had to convert the string into a number, I just want to know how it figured that the string was equal to 5? Other than that, I'm pretty sure I won't ever be making that kind of mistake in my scripts since I now understand how it works. Thanks again! Quote Link to comment https://forums.phpfreaks.com/topic/237088-beginner-help-adding-a-number-with-a-variable/#findComment-1218593 Share on other sites More sharing options...
requinix Posted May 22, 2011 Share Posted May 22, 2011 Linky More specific linky Short answer: PHP tries to pull a number from the beginning of the string. (If it can't then it returns 0.) Your string just happened to start with "5" so that's what you got back. Quote Link to comment https://forums.phpfreaks.com/topic/237088-beginner-help-adding-a-number-with-a-variable/#findComment-1218596 Share on other sites More sharing options...
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.