PHP Operators Precedence - PHP Operators - session php

PHP Operators Precedence

/ins>

Operator Precedence in PHP

You would have probably learned about BOD-MAS, a mnemonic that specifies the order in which a calculator or a computer performs a sequence of mathematical operations.

Brackets , order , Division , Multiplication , Addition , and Subtraction.

PHP’s precedence rules are tough to remember.
Parentheses always have the highest precedence, so wrapping an expression in these will force PHP to evaluate it first, when using multiple sets of parentheses.

Here is an example, consider the expression

Output
with parentheses :3

without parentheses :31.8

Explanation of output

With parentheses:-
First step: 4 multiplied by 8 is 32.
Second step: 2 subtracted from 32 is 30.
and final step: 30 divided by 10 is 3.

Without parentheses:-
First step: 2 divided by 10 is 0.2.
Second step: 4 multiplied by 8 is 32.
and final step: 0.2 subtracted from 32 is 31.8