claudiogc Posted August 29, 2014 Share Posted August 29, 2014 Hey, guys! Why the output of my code in both "echo" is only the number "2"? <?php $x = 4; $y = 2; //Addition. echo "Addtion: " . $x + $y . "<br>"; echo 'Addtion: ' . $x + $y . '<br>'; ?> Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/290737-simple-question-about-addition-of-two-numbers/ Share on other sites More sharing options...
Jacques1 Posted August 29, 2014 Share Posted August 29, 2014 (edited) You somehow assume that addition takes precedence over string concatenation, but that's not the case. They have equal precedence and are evaluated from left to right. It's generally a bad idea to rely on the internal precedence of operators (except maybe for obvious cases like addition and multiplication). Use parentheses. Edited August 29, 2014 by Jacques1 1 Quote Link to comment https://forums.phpfreaks.com/topic/290737-simple-question-about-addition-of-two-numbers/#findComment-1489323 Share on other sites More sharing options...
claudiogc Posted August 29, 2014 Author Share Posted August 29, 2014 (edited) Problem fixed. Thanks! They have equal precedence and are evaluated from left to right. If is the case why is he not showing "Addition: 4"? Because he should concat "Addition" with "4" and after that i have no idea. Edited August 29, 2014 by claudiogc Quote Link to comment https://forums.phpfreaks.com/topic/290737-simple-question-about-addition-of-two-numbers/#findComment-1489324 Share on other sites More sharing options...
kicken Posted August 29, 2014 Share Posted August 29, 2014 If is the case why is he not showing "Addition: 4"? Because he should concat "Addition" with "4" and after that i have no idea. If you evaluate it one operator at a time and sub the results into the original equation you can see what happens. echo 'Addtion: ' . $x + $y . '<br>'; Evaluate the first . operator, result is: 'Addition: 4', so then you have: echo 'Addtion: 4' + $y . '<br>'; Evaluate the + operator. PHP try and convert the first operand to a number but since the string is not a valid number you get zero. Now you have: echo 2 . '<br>'; Which gives you a final output of '2<br>'; 1 Quote Link to comment https://forums.phpfreaks.com/topic/290737-simple-question-about-addition-of-two-numbers/#findComment-1489325 Share on other sites More sharing options...
claudiogc Posted August 29, 2014 Author Share Posted August 29, 2014 PHP try and convert the first operand to a number but since the string is not a valid number you get zero. Did not understand this part. "PHP try and convert the first operand to a number" The number 2? Because the real first operand of the operation, number 4, already is part of the other string. What happened to "+"? "but since the string is not a valid number you get zero." What string? Quote Link to comment https://forums.phpfreaks.com/topic/290737-simple-question-about-addition-of-two-numbers/#findComment-1489331 Share on other sites More sharing options...
kicken Posted August 29, 2014 Share Posted August 29, 2014 The + operator applies to 'Addition: 4' and 2. 'Addition: 4' is a string, not a number so PHP will attempt to convert it. Since it is not possible to convert it however you end up with zero so the end result is you have 0 + 2. Quote Link to comment https://forums.phpfreaks.com/topic/290737-simple-question-about-addition-of-two-numbers/#findComment-1489332 Share on other sites More sharing options...
Psycho Posted August 29, 2014 Share Posted August 29, 2014 (edited) Step 1:echo [ "Addtion: " . $x ] + $y . "<br>"; PHP CONCATENATES the String "Addition" and the number 4 creating the string "Addition: 4", which results in:echo "Addtion: 4" + $y . "<br>"; //Step 2:echo [ "Addtion: 4" + $y ] . "<br>"; PHP ADDS the string "Addition: 4" with the number 2 Since the string is not a number, PHP converts it to one. Since it begins with an alpha character it converts it to 0. Thus, you get [ 0 + 2]. Result:echo 2 . "<br>"; Step 3:echo [ 2 . "<br>" ]; PHP CONCATENATES the Number 2 and the string "<br>"Result echo "2 <br>"; Edited August 29, 2014 by Psycho Quote Link to comment https://forums.phpfreaks.com/topic/290737-simple-question-about-addition-of-two-numbers/#findComment-1489338 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.