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!

1 Like

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

2 Likes

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.

PHP: Operator Precedence - Manual

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

1 Like

94 - 4.25 + 7 - (23.50 + 23.50 * .2) + 20 / 4;

Why do we have to * .2 on (23.50 + 23.50 * .2)? What is the meaning of .2?

@radeakanta

0.2 is the result of 20%(=20/100=0.2).

From the exercise:

  • I went out for a meal. The bill was $23.50, but I also gave a 20% tip.

Since the % symbol is reserved for the Module operator, I believe we cannot use it to show percentage.

So,
20% of $23.50
= 0.2 * 23.50
= $4.70.

I think the solution is pretty unfair in my opinion, since the the exercise is not using the same method that’s been shown on the example, the PEMDAS really not implemented, you can’t just have

I went out for a meal. The bill was $23.50, but I also gave a 20% tip.

and make the 20% as a direct 1.2, we should count it first, or at least you could make that part as the parentheses ones or something so that would be good maybe like (23.50 * (20 / 100))…you know something like that