Tutorial 16: Arithmetic Operators

This is where we start getting into some math! It's very basic, but incredibly useful. Check out this table, provided by PHP.net:

Example Name Result
\$a + \$b Addition Sum of \$a and \$b
\$a - \$b Subtraction Difference of \$a and \$b
\$a * \$b Multiplication Product of \$a and \$b
\$a / \$b Division Quotient of \$a and \$b
\$a % \$b Modulus Remainder of \$a divided by \$b

The modulus operator is usually a pretty confusing concept at first, but mathforum.org provides a pretty good explanation here:

The "mod" operator in computer languages is simply the remainder. For example,

17 mod 3 = 2

because

17 / 3 = 5 rem 2

which in turn means

17 = 3 * 5 + 2

Here is an example of how you would use one of the above arithmetic operators:

<?php

\$birthYear = 1988;
\$thisYear = date('Y');

\$myAge = (\$thisYear - 1988);

echo \$myAge;

?>

Feel free to experiment with all of the above arithmetic operators with some PHP using the practice.php page provided in this folder.