Math Operators
These tutorials are about PHP and its use for server-side Web programming.
Arithmetic Operators
Arithmetic operators fall into two categories, those that modify the value of an expression or set of expressions and those that take the value of one expression and assign it to another expression. These latter operators are also called assignment operators.
The following arithmetic operators are the basic math operators. They do not alter the values of their operands, and therefore need to have their result assigned to some variable in order to have any real effect.
- Addition (
+ )
- The addition operator adds two numeric operands or concatenates two string operands. Values need to be convertible to a number to work with the addition operator.
- Subtraction (
- )
- The subtraction operator subtracts the value of the second operand (the one after the minus sign) from the value of the first operand (the one before the minus sign). It only works on numbers.
- Multiplication (
* )
- The multiplication operator multiplies the numeric values of two operands together.
- Division (
/ )
- The division operator divides its first operand by its second operand. Division of integers that do not divide evenly will yield a floating-point result. Division by zero yields
Infinity or -Infinity. The expression 0/0 evaluates to Nan.
- Modulo (
% )
- The modulo operator divides its first operand by its second and returns the modulus. The modulus in division is the remainder, or what is left over.
- Unary Minus (
- )
- The unary minus is a unary operator that negates the operand immediately following it, which is to say, converts it to its negative equivalent.
- Unary Plus (
+ )
- This operand does nothing, but exists to balance the unary minus. No, really.
The following arithmetic operators are unary operators that modify the value of their single operand.
- Increment (
++ )
- The increment operator increments the numeric value of its operand by one. If placed before the operand, it returns the incremented value. If placed after the operand, it returns the original value and then increments the operand.
- Decrement (
-- )
- The decrement operator decrements the numeric value of its operand by one. In other words, it counts backwards. If placed before the operand, it returns the decremented value. If placed after the operand, it returns the original value and then decrements the operand.
Unlike some languages, PHP allows you to increment certain strings as well as integers. Thus, the following code is legal.
$i = 'a';
$i++;
The incrementing and decrementing works with strings and numbers is as follows:
- All numbers are either incremented or decremented by one each time the statement is invoked. Once a digit reaches a values of nine, it rolls back over to zero and the one is carried left to the next position. If it is decremented to zero, it rolls over to nine and one is subtracted from the position to the left.
- All letters in the range of A through Z, either upper or lower case, are incremented or decremented by one each time the statement is invoked. Thus 'A'++ would become 'B', and so forth. When a given character reaches a value of 'Z' it is rolled back over to 'A' and the one is carried left to the next position. If it is decremented to 'A', it rolls over to 'Z' and one is subtracted from the position to the left.
- This even works for mixed strings.
Increment and decrement examples:
'5'-- == '4'
'E'++ == 'F'
'xyz'++ == 'xza'
'xYz'++ == 'xZa'
'L9'++ == 'M0'
'12a'-- == '11z'
The rest of the arithmetic operators are assignment operators. They assign the value of the operand on the right to the operand on the left. There are two types of assignment operators. The first is the simple assignment operator. It is represented by the single equals sign ( = ). It is the assignment operator we are most familiar with.
The value to the left of the assignment operator must be an L-value, which is an expression that can have a value assigned to it.
$a = $b + c$;
$validCheck = ($testVar == $myCond);
PHP also allows another type of operator that perform with is called assignment with operation. What this means is that they include both a math operator and an assignment operator in one operation. What is the purpose of such an operator? Well, many expressions you will end up writing may take the form of $a = $a + $b;, or the value of the first operand equals the value of the first operand as modified by a second operand. PHP allows a shorthand notation for these. $a = $a + $b; is equivalent to $a += $b. The same shortcut can be applied to subtraction, multiplication, division and modulus.
$a += $b; is the same as $a = $a + $b;
$a -= $b; is the same as $a = $a - $b;
$a *= $b; is the same as $a = $a * $b;
$a /= $b; is the same as $a = $a / $b;
$a %= $b; is the same as $a = $a % $b;
Note that in each of these, the two characters form a single operator, and cannot have a space between them.
These pages can be found at:
[http://academ.hvcc.edu/~kantopet/]
Copyright 2003 -- Peter L. Kantor[kantopet@hvcc.edu]
Last Updated January 2003