Operators

Operators let you specify mathematical operations to perform, such as addition, subtraction, multiplication, and division. But several other types of operators exist too, such as the string, comparison, and logical operators. Math in PHP looks a lot like plain arithmetic—for instance, the following statement outputs 8:

echo 6 + 2;

Before moving on to learn what PHP can do for you, take a moment to learn about the various operators it provides.
Arithmetic operators Arithmetic operators do what you would expect—they are used to perform mathematics.
You can use them for the main four operations (add, subtract, multiply, and divide) as well as to find a modulus (the remainder after a division) and to increment or decrement a value.

Operator Description    Example
+        Addition       $j + 1
-        Subtraction    $j - 6
*        Multiplication $j * 11
/        Division       $j / 4
%        Modulus        $j % 9
++       Increment      ++$j
--       Decrement      --$j
**       Exponentiation $j**2

Assignment operators
These operators assign values to variables. They start with the very simple = and move on to +=, -=, and so on.
The operator += adds the value on the right side to the variable on the left, instead of totally replacing the value on the left.
Thus, if $count starts with the value 5, the statement:
$count += 1;
sets $count to 6, just like the more familiar assignment statement:
$count = $count + 1;

Leave a Reply

Your email address will not be published. Required fields are marked *