Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lära Math Expressions | Första Bekantskapen
Introduktion till PHP

Math Expressions

Svep för att visa menyn

We also can write math expressions. The sequence of actions will work the same as in mathematics, where 2 + 2 * 2 = 6, because the multiplication is done before the addition.

main.php

main.php

123
<?php echo 2 + 2 * 2; ?>

Let's look at a more complex example:

main.php

main.php

123
<?php echo (12 + 6) * 2 - 10 / 5 + 2; ?>
bd29f495-926c-4fe6-8184-7122593c3413

First, in the expression (12 + 6) * 2 - 10 / 5 + 2, we perform the operations inside the parentheses. Adding 12 and 6 gives us 18. So, the expression now looks like 18 * 2 - 10 / 5 + 2.

Next, according to the rules of operator precedence, we handle multiplication and division from left to right. First, we calculate 18 * 2, which equals 36. Then, we compute 10 / 5, which equals 2. Thus, our expression transforms into 36 - 2 + 2.

Moving forward, we perform subtraction: 36 - 2, resulting in 34. Finally, we add 2 to 34, giving us the final result of 36.

Therefore, the expression (12 + 6) * 2 - 10 / 5 + 2 evaluates to 36.

question mark

Consider the following PHP expression:

Vänligen välj det korrekta svaret

Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 1. Kapitel 5

Fråga AI

expand

Fråga AI

ChatGPT

Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal

Math Expressions

We also can write math expressions. The sequence of actions will work the same as in mathematics, where 2 + 2 * 2 = 6, because the multiplication is done before the addition.

main.php

main.php

123
<?php echo 2 + 2 * 2; ?>

Let's look at a more complex example:

main.php

main.php

123
<?php echo (12 + 6) * 2 - 10 / 5 + 2; ?>
bd29f495-926c-4fe6-8184-7122593c3413

First, in the expression (12 + 6) * 2 - 10 / 5 + 2, we perform the operations inside the parentheses. Adding 12 and 6 gives us 18. So, the expression now looks like 18 * 2 - 10 / 5 + 2.

Next, according to the rules of operator precedence, we handle multiplication and division from left to right. First, we calculate 18 * 2, which equals 36. Then, we compute 10 / 5, which equals 2. Thus, our expression transforms into 36 - 2 + 2.

Moving forward, we perform subtraction: 36 - 2, resulting in 34. Finally, we add 2 to 34, giving us the final result of 36.

Therefore, the expression (12 + 6) * 2 - 10 / 5 + 2 evaluates to 36.

Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 1. Kapitel 5
some-alt