Arithmetic Operators
PHP Arithmetic operators are very similar to C and c++ operators, if you know these languages, operators will be familiar and easy to recognize.
These are the Arithmetic operators that can be applied to variables and numeric constants.
| Operator | Name | Example |
| + | Addition | 2 + 4 |
| - | Subtraction | 6 - 2 |
| * | Multiplication | 5 * 3 |
| / | Division | 15 / 3 |
| % | Modulus | 43 % 10 |
Examples
<?php
$var= 5 + 5; // 10
$var = 5 - 5; // 0
$var = 5 + 5 - (5 + 5); // 0
$var = 5 * 5; // 25
$var = 10 * 5 - 5; // 45
Modulus
for ($i = 1; $i <= 10; $i++) {
if(($i % 2) == 1) //odd
{echo $i."is odd number";}
else //even
{echo $i."is even number";}
}
?>
|