Chapter 6. Operators
In this chapter, we look at operators, which are the symbols such as + (adding), - (subtracting), and * (multiplying).
Operators are like functions in that they do something with values, but they use symbols rather than function names. In the equation 2 + 3
, the 2 and the 3 are both operands, and the + is the operator. There are three types of operators: unary, binary, and ternary, which take one, two, and three operands respectively. As you can see, the + operator (used to add numerical values) is a binary operator, because it takes two variables as input.
Arithmetic Operators
The arithmetic operators handle basic numerical operations, such as addition and multiplication. The full list is shown in Table 6-1.
Table 6-1. The arithmetic operators
+ |
Addition |
Returns the first value added to the second: |
- |
Subtraction |
Returned the second value subtracted from the first: |
* |
Multiplication |
Returns the first value multiplied by the second: |
/ |
Division |
Returns the first value divided by the second: |
% |
Modulus |
Divides the first value into the second, then returns the remainder: |
+= |
Shorthand addition |
Adds the second value to the first: |
-= |
Shorthand subtraction |
Subtracts the second value from the first: |
*= |
Shorthand multiplication |
Multiplies the first value ... |
Get PHP in a Nutshell now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.