Home >Backend Development >PHP Tutorial >Three types of operators in PHP_PHP tutorial
An operator can produce another value by giving one or more values (in programming jargon, expression) value (thus the entire structure becomes an expression).
The first one is a unary operator, which only operates on one value, such as ! (inversion operator) or ++ (plus one operator).
Example
1. Usage of ++i (take a=++i, i=2 as an example)
First add 1 to the i value (that is, i=i+1), and then assign it to the variable a (that is, a=i),
Then the final a value is equal to 3 and the i value is equal to 3.
So a=++i is equivalent to i=i+1, a=i
2. Usage of i++ (take a=i++, i=2 as an example)
First assign the i value to the variable a (that is, a=i), then add 1 to the i value (that is, i=i+1),
Then the final a value is equal to 2 and the i value is equal to 3.
So a=i++ is equivalent to a=i , i=i+1
3. ++i and i++
a=++i is equivalent to i++, a=i
a=i++ is equivalent to a=i, i++
4. When ++i and i++ are used alone, they are equivalent to i=i+1
If assigned to a new variable, ++i first adds 1 to the i value, and i++ first assigns i to the new variable
The second type is a binary operator that accepts two values, such as the familiar arithmetic operators + (addition) and - (subtraction), which are most PHP operators
$a =1+2;
$b =3-1;
The third type is the ternary operator, which accepts three values. It should be used to select one of two expressions based on one expression, rather than selecting between two statements or program routes. . (It may also be called a conditional operator, which may be more appropriate)
The code format is as follows: (expr1) ? (expr2) : (expr3);
For example: $page = !empty( $_GET['page'] ) ? $_GET['page'] : 1;