Reputation: 95
As PHP's Manual shows, the following operators have the same priority (left associativity):
* / %
So, It means:
echo 2 / 5 * 3
must display 7.5! because the multiplication will perform first, 5 * 3 = 15
then the quotient will divided by 2.
But when I run that code, PHP code outputs 1.2!
Could anyone please to understand what's going on?
Upvotes: 1
Views: 110
Reputation: 38584
There is an order to execute arithmetic operations. which call short PEMDAS
()
- brackets/
- deviation*
- multiplication+
- add-
- minThis will(2 / 5 * 3
) execute in above order
So what happen on here 2 / 5 * 3
2/5
= 0.40.4*3
= 1.2To fulfill your requirement
5 * 3
= 1515 / 2
= 7.5So you have to do (5 * 3) / 2
or 2 / (5 * 3)
Upvotes: 1
Reputation: 1507
As you can see here: http://php.net/manual/en/language.operators.precedence.php
the operator * / and % has the same precedence. * has the same precedence as /.
Upvotes: 0
Reputation: 5939
Same priority means that everything will happen from left to right.
Meaning that it won't multiply first. The order in which * / %
was written in the manual does not matter.
Also, by your logic, you should be getting 1.(3) but that's not the point.
Upvotes: 3