Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Basic Math Operations | First Acquaintance
Introduction to PHP

Sveip for å vise menyen

book
Basic Math Operations

Numbers


You already know how to use the echo function to output text. But what about numbers and mathematical calculations?

You can also use echo to display these. When you want to output the result of a mathematical expression, there's no need to use quotes (" " or ' '). Simply write the expression directly inside echo, and it will be evaluated and displayed on the screen.

php

main

copy
12345
<?php echo 'Two'; // This outputs the string "Two" echo '2'; // This outputs the string "2" echo 2; // This outputs the number 2 ?>

If you print a number and a string to the console, they will look the same, which is something they have in common. However, there are things we can do with numbers that we can't do with strings, and vice versa. For instance, we can apply mathematical operations to numbers.

Math Operations


Almost no program can do without mathematical calculations, so PHP supports the following mathematical operations:

The code below performs basic arithmetic operations on different numbers and outputs the results to the screen.

Output the result of adding 5 and 3:

php

main

copy
123
<?php echo 5 + 3; // Outputs: 8 ?>

Output the result of subtracting 7 from 10:

php

main

copy
123
<?php echo 10 - 7; // Outputs: 3 ?>

Output the result of dividing 15 by 3:

php

main

copy
123
<?php echo 15 / 3; // Outputs: 5 ?>

Output the result of multiplying 4 by 6:

php

main

copy
123
<?php echo 4 * 6; // Outputs: 24 ?>

Output the remainder of dividing 17 by 5:

php

main

copy
123
<?php echo 17 % 5; // Outputs: 2 ?>
Oppgave

Swipe to start coding

Here's your assignment: compute the following and print the outcome: 1512 divided by 27.

Løsning

Switch to desktopBytt til skrivebordet for virkelighetspraksisFortsett der du er med et av alternativene nedenfor
Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 1. Kapittel 4
Vi beklager at noe gikk galt. Hva skjedde?

Spør AI

expand
ChatGPT

Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår

book
Basic Math Operations

Numbers


You already know how to use the echo function to output text. But what about numbers and mathematical calculations?

You can also use echo to display these. When you want to output the result of a mathematical expression, there's no need to use quotes (" " or ' '). Simply write the expression directly inside echo, and it will be evaluated and displayed on the screen.

php

main

copy
12345
<?php echo 'Two'; // This outputs the string "Two" echo '2'; // This outputs the string "2" echo 2; // This outputs the number 2 ?>

If you print a number and a string to the console, they will look the same, which is something they have in common. However, there are things we can do with numbers that we can't do with strings, and vice versa. For instance, we can apply mathematical operations to numbers.

Math Operations


Almost no program can do without mathematical calculations, so PHP supports the following mathematical operations:

The code below performs basic arithmetic operations on different numbers and outputs the results to the screen.

Output the result of adding 5 and 3:

php

main

copy
123
<?php echo 5 + 3; // Outputs: 8 ?>

Output the result of subtracting 7 from 10:

php

main

copy
123
<?php echo 10 - 7; // Outputs: 3 ?>

Output the result of dividing 15 by 3:

php

main

copy
123
<?php echo 15 / 3; // Outputs: 5 ?>

Output the result of multiplying 4 by 6:

php

main

copy
123
<?php echo 4 * 6; // Outputs: 24 ?>

Output the remainder of dividing 17 by 5:

php

main

copy
123
<?php echo 17 % 5; // Outputs: 2 ?>
Oppgave

Swipe to start coding

Here's your assignment: compute the following and print the outcome: 1512 divided by 27.

Løsning

Switch to desktopBytt til skrivebordet for virkelighetspraksisFortsett der du er med et av alternativene nedenfor
Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 1. Kapittel 4
Switch to desktopBytt til skrivebordet for virkelighetspraksisFortsett der du er med et av alternativene nedenfor
Vi beklager at noe gikk galt. Hva skjedde?
some-alt