Chapter 5. Working with Numbers

In this lesson you will learn about some of the numeric manipulations you can perform in PHP.

Arithmetic

As you would expect, PHP includes all the basic arithmetic operators. If you have not used another programming language, the symbols used might not all be obvious, so we'll quickly run through the basic rules of arithmetic in PHP.

Arithmetic Operators

Addition is performed with the plus symbol (+). This example adds 6 and 12 together and displays the result:

echo 6 + 12;

Subtraction is performed with the minus symbol (-), which is also used as a hyphen. This example subtracts 5 from 24:

echo 24 - 5;

The minus symbol can also be used to negate a number (for example, 20).

Multiplication is performed with the asterisk ...

Get Sams Teach Yourself PHP in 10 Minutes now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.