FAQ: PHP Numbers - Order of Operations

This community-built FAQ covers the “Order of Operations” exercise from the lesson “PHP Numbers”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Learn PHP

FAQs on the exercise Order of Operations

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

Hi!
In this lesson, the order of operators is described, but the Modulo operator (%) isn’t mentioned. The order explained is:

  1. ()
  2. **
  3. * and /
  4. + and -

I suppose the modulo operator is at number 2, next to exponentiation, but want to make sure.

Thanks!

It’s actually at number 3 on your list. It’s basically treated like / but returns the remainder instead of the quotient.

Ok, thank you for clearing that out!

Definitely bookmark this page for easy reference. Until this stuff is cemented in our minds, it can never hurt to double check.

https://www.php.net/manual/en/language.operators.precedence.php

When two operators have equal precedence, association is left to right. This can have a bearing on the result so order of placement becomes the precedence determiner.

 > 10 % 3 / 4
<- 0.25
 > 10 / 4 % 3
<- 2.5
 > 10 % 3 * 4
<- 4
 > 10 * 4 % 3
<- 1
 > 

Times and powers are not equal precedence so can be written however they occur without changing the result.

 > 2 ** 2 * 4
<- 16
 > 4 * 2 ** 2
<- 16
 > 

Totally off topic and different language, but the gist is the same.

>>> list(map(lambda x: 10 / x % 3, range(1, 10)))
[1.0, 2.0, 0.3333333333333335, 2.5, 2.0, 1.6666666666666667, 1.4285714285714286, 1.25, 1.1111111111111112]
>>> list(map(lambda x: 10 % 3 / x, range(1, 10)))
[1.0, 0.5, 0.3333333333333333, 0.25, 0.2, 0.16666666666666666, 0.14285714285714285, 0.125, 0.1111111111111111]
>>> 

Note the relationship between corresponding terms in each list. I’d love to see these two graphs plotted if there are any matplotlib savvy learners. Apologize for bringing Python into the mix, but it is more for convenience. The point is the relation between these two graphs, which is what I found kind of interesting, nothing to do with the OP, save the illustration of precendence with two curves.

equal_precedence_order