$total = 30 - $nr1 / 13 - $nr2 - 6 * $nr3 - 3
I know we learned that in school but what is first (+ or - or * or /), where are the brackets or do i even need them ?
$total = 30 - $nr1 / 13 - $nr2 - 6 * $nr3 - 3
I know we learned that in school but what is first (+ or - or * or /), where are the brackets or do i even need them ?
You put brackets to priortizes what should be calculated first. In math though it starts from division, multiplication, subtraction and finally addition. So, here is the order of precedence for these:
You can however override that rule by specifying brackets, for example you might want to have addition calculated first before anything else.
More Info:
First partentheses are calculated. Then multiplication and division. Then plus and minus. If you write say a*b/c, because multiplication doesn't precede division, nor does division precede multiplication, the computer will calculate it in the order it stands. So it will first calculate a*b and then divide that by c.
$total = 30 - ($nr1 / 13) - $nr2 - (6 * $nr3) - 3
I don't think extra brackets would harm. I always use them to improve readability
the +- and */ pairs are of equal precedence. they are evaluated left to right.