PHP Operators - Arithmetic Operators
(Page 3 of 6 )
You have been using most of the arithmetic operators since childhood. That and your fingers and toes. But don't worry; PHP is smarter than you. It's prettier than you. And according to some of the other programming languages, it really gets around. ASP is really jealous, but don't tell it I said so.
Let's look at some examples.
<html>
<body>
<?php
$add = 1 + 0;
$subtract = 2 - 1;
$multiply = 1 * 1;
$division = 2 / 2;
$modulus = 5 % 2;
echo $add;
echo $subtract;
echo $multiply;
echo $division;
echo $modulus;
?>
</body>
</html>
The above code would print out all the values of our variables:
1
1
1
1
1
A Brief Note About My Dear Friend...The Modulus
The majority of the tutorials I've read on PHP never really explained Modulus in simple terms and so I never knew what it did. Maybe I'm just an idiot. But if there are other idiots out there, then you are in luck, because I am going to put this in very simple terms. Modulus gives you the remainder in division. Period. If I divide 2 / 2, there is no remainder. If I divide 3 /7 there is a remainder of 1. Modulus would return that 1.
Next: Incremental Operators >>
More PHP Articles
More By James Payne