What is the PHP arithmetic operator?
Before we introduce arithmetic operators , let's look at what operators are, which are symbols used to perform program code operations, often used to evaluate variables, constants, or data, which is a specified operation on a value or a set of values. PHP operators: Arithmetic operators, string operators, assignment operators, bitwise operators, logical operators, comparison operators, increment or decrement operators, error control operators. In the back we will explain to you separately, let us first explain the arithmetic operators in the PHP operator.
Introduction to the various symbols of arithmetic operators:
Arithmetic operators are symbols that handle arithmetic operations. The most commonly used arithmetic operators in the processing of numbers are the following table
| Operator |
Description |
Example |
| + |
Addition operation |
$a + $b |
| - |
Subtraction operations |
$a-$b |
| * |
Multiplication operations |
$a * $b |
| / |
Division operation |
$a/$b |
| % |
Take the remainder operation |
$a% $b |
| ++ |
Increment operation |
$a ++,++ $a |
| -- |
Decrement operation |
$a--,--$a |
PS: Use the% remainder in arithmetic operators, and if the divisor ($a) is negative, the result is a negative value.
This is to say the last two increment/decrement operators, which are mainly operated on a single variable.
There are two ways to use the increment/decrement operator:
The first is to increase or decrease the variable by 1 before assigning the value to the original variable, which is called the forward increment/decrement operator.
The second type is to put the operator after the variable, return the current value of the variable, and then increase or decrease the current value of the variable by one, which we call the post increment/decrement operator
Arithmetic operator Usage instance
The following examples are calculated using several operators in the table above, with the following code:
<?php$a = 8; $b = 2; $c = 3;echo $a + $b. " <br>\n ", Echo $a-$b." <br>\n ", Echo $a * $b." <br>\n "; echo $a/$b." <br>\n "; echo $a% $c." <br>\n "; $a ++;echo $a." <br>\n "; + + $a; echo $a." <br>\n "; $c--;echo $c;? >
Running results such as
The above example is our simple application of arithmetic operators, and more on the PHP operator topic . In the next section we will describe in detail the concept and usage of string operators.
Related Video Tutorials recommended:
php.cn (4)-php video tutorial: operator (i): arithmetic operator, string operator, assignment operator