All lessons

Numbers & Math

6 min · PHP

Do math with variables and the basic operators.

PHP · Numbers & Math0/5 cleared

Enemy 1 of 5

Arithmetic operators

PHP supports the same four basic arithmetic operators you'd expect from a calculator: + for addition, - for subtraction, * for multiplication, and / for division. You can apply them directly to numbers or to variables that hold numbers, and PHP evaluates them following the usual order of operations (multiplication and division happen before addition and subtraction, unless you use parentheses to force a different order).

PHP actually has two separate numeric types under the hood — integers (whole numbers like 5) and floats (decimals like 5.0) — but you rarely have to think about which one you're using day to day, since PHP converts between them automatically when an operation needs it. Dividing two integers that don't divide evenly, for example, quietly produces a float.

The example also shows something PHP-specific: echo can take several comma-separated values and print them one after another, with no automatic separator between them. That's different from concatenating with . — commas after echo don't glue strings into one value, they just print each argument in sequence, which is a subtle but useful distinction once you start mixing text and numbers in one echo statement.

You
Syntax Skeleton

Loading editor...

Ln 1, Col 1Spaces: 4
Sign up to fightTarget output: 15