Free php course. Sign Up for tracking progress →

PHP: Function composition

What if you need to solve something like this: 3 * 5 - 2?


print_r(3 * 5 - 2);

Another exmaple:


print_r(2 * 4 * 5 * 10);

As you see, operations can be combined. This allows us to calculate more and more complex expressions. Such property is called composition. Composition of arithmetic operations in programming is identical to that you learned in school. As you will find out later, the operation composition applies to all operations, not just arithmetic ones.


Write a program that calculates the result of 8 / 2 + 5 - -3 / 2. Do not calculate anything by hand, all the calculations must be done by your code.

Note that the interpreter does the calculations in the correct order: division and multiplication first, and only then addition and subtraction. Sometimes we need to modify this order, and we'll talk about how to do it later.

If you got stuck and don't know what to do, you can ask a question in our huge and friendly community