# PHPOrder of Operations - Formulas

#### AnderMedia

##### Coder
So, I have been studying up on my PHP and something I always run across is the method of which a formula is read and processed. Which is why I love PHP so much, I think it is one of the most dynamic languages when it comes to SQL and updating your database using formulas. Now I wanted to get clarification for myself and others who may view this thread. The order of operations in which the formulas are processed follow a syntax of it's own. When I was in school learning basic math and it's order of operations we used a phrase "Please Excuse My Dear Aunt Sally". And this acronym P.E.D.M.A.S helps one remember math's order of operations for:
• P - (Please) - Parentheses
• E - (Excuse) - Exponents
• M - (My) - Mulitplication
• D - (Dear) - Division
• A - (Aunt) - Addition
• S - (Sally) - Subtraction
Now, I haven't seen or experienced using exponents in a PHP formula myself, but does this match the order of operations for PHP formulas also?
Could some one clarify and perhaps apply a link to some reference that would explain it in much more depth?

Thank you much.

Kind regards, Matt

(Edit:
Just a quick update, I found this helpful tutorial video on YouTube about Numbers, and it explained "Powers" which is also exponents by definition.
You would use ** for the Power, so 5**3 = 125. Any input is still appreciated. YouTube Tutorial - PHP Numbers

Now, I haven't seen or experienced using exponents in a PHP formula myself, but does this match the order of operations for PHP formulas also?
Could some one clarify and perhaps apply a link to some reference that would explain it in much more depth?
Yes, PHP follows the order of operations.

Here's a link with a table showing the precedence of operators

I'm reasonably sure that all good languages follow this order.
But if you have a formula with multiple operations and you're not quite sure of their order (or even have to think about it), why not take the pragmatic way out and put in some parentheses ? Personally I find `x = (a*b) + c` clearer than `x = a * b + c`.

To reiterate @cbreemer 's point, all modern programming languages are compiled to follow the order of operations when it comes to math. As a general rule of thumb, make it a habit of putting your equations inside of parenthesis. This makes things easier to debug if you don't get the right output.
ex:
JavaScript:
``var x = Math.pow(((( a*b ) + ( c - d )) / 2 ) , 2);``

Just make sure you keep count of your parenthesis XD

Just make sure you keep count of your parenthesis XD
And make sure you don't use more than are needed to make things clearer. I would simplify Antero's example like this

JavaScript:
``var x = Math.pow( ((a*b) + c - d) / 2, 2);``

If the parentheses get any more profuse, and you need to start counting, I think it becomes counterproductive. Then I'd just as soon use one or more intermediate variables. But really, this is a matter of personal taste.

One could get dizzy with all the parentheses. Thanks all. I was pretty sure that all languages are compiled to follow this order.