1). I have 4 numbers to add up and display result with a string concate to them For example $first_number = 10; $second_number = 20; $third_number = 30; $fourth_number = 40; $direct_text = 'addition of 4 numbers is = '; I have to display Print(‘addition of 4 numbers is =’. $first_number + $second_number +$third_number + $fourth_number); But the concated sing is not displayed also the last variable ($first_number) is not added to string I mean resuld should like this addition of 4 numbers is = 100 but result is 90 neither the prefixed string nor the last value after dot oprator is added in the result If I remove the concated string and dot then the addition is correct I mean correct result Also when I do the add operation in a variable and then concate it to the string then the result is correct I mean when the is like this $first_number = 10; $second_number = 20; $third_number = 30; $fourth_number = 40; $total = $first_number + $second_number +$third_number + $fourth_number; $direct_text = 'addition of 4 numbers is = '; print($direct_text . $total); then result is as required. I mean it is addition of 4 numbers is = 100 but I want to know that why when we do addition in the same code where concate the string then it not work properly kindly let me know
Use this; Print('addition of 4 numbers is ='. ($first_number + $second_number + $third_number + $fourth_number)); PHP:
Thanks for reply it is really working now. But i want to know the reason why the number are not added correctly and not displaying string when not enclosing in the parenthesis i just want to know the logic behind it
A word of caution - the dot operator has the same precedence as + and -, which can yield unexpected results. Example: <php $var = 3; echo "Result: " . $var + 3; ?> The above will print out "3" instead of "Result: 6", since first the string "Result3" is created and this is then added to 3 yielding 3, non-empty non-numeric strings being converted to 0. To print "Result: 6", use parantheses to alter precedence: <php $var = 3; echo "Result: " . ($var + 3); ?>
Thanks so much i now completly solve the issue as well get the reason of error ur answer are very informative thanks again