• Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

PHP Order of Operations - Formulas

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
 

Johna

Frontend Web Developer
Staff Team
Guardian
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
 

cbreemer

King Coder
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.
 

Antero360

King Coder
Staff Team
Security Analyst
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
 

cbreemer

King Coder
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.
 
Top