Operators in PHP --- Arithmetic Operators, logical operators, value assignment operators, comparison operators, and operator assignment
1. Arithmetic Operators
Common Arithmetic Operators
| Operation Type |
Operator |
Example |
Result |
| Inverse Operation |
- |
-$ |
Returns a negative value of $. |
| Addition operation |
+ |
$ A + $ B |
Returns the sum of $ a and $ B. |
| Subtraction |
- |
$ A-$ B |
Returns the difference between $ a and $ B. |
| Multiplication |
* |
$ A * $ B |
Returns the product of $ a and $ B. |
| Division |
/ |
$ A/$ B |
Returns the quotient of $ a and $ B. |
| Remainder operation |
% |
$ A % $ B |
Returns the remainder of $ a and $ B. |
2. logical operators
Logical operators in PHP
| Operation Type |
Operator |
Example |
Result |
| Logic and |
& Or and |
$ A & $ B or $ a and $ B |
If both $ a and $ B are true, true is returned. Otherwise, false is returned. |
| Logic or |
| Or |
$ A | $ B or $ a or $ B |
If $ a or $ B is true, true is returned. Otherwise, false is returned. |
| Logic exclusive or |
Xor |
$ A xor $ B |
If $ a is true, $ B is false, or $ a is false, $ B is true. Otherwise, false is returned. |
| Non-logical |
! |
! $ |
Returns true if $ a is false; otherwise, false. |
3. Value assignment operator
The value assignment operator "=" is the most basic operator in PHP. That is, the value of the right expression "=" is assigned to the number of operations on the left.
In addition, compound assignment operators are also commonly used in PHP.
Compound assignment operator
| Operation Type |
Operator |
Example |
Result |
| Addition assignment |
+ = |
$ A + = 5 |
$ A and the sum of 5 are assigned to $. |
| Subtraction assignment |
-= |
$ A-= 5 |
$ A minus 5 is assigned to $. |
| Multiplication assignment |
* = |
$ A * = 5 |
$ A multiplied by the product of 5 and assigned to $ |
| Division assignment |
/= |
$ A/= 5 |
$ A is assigned to $ a by the operator of 5. |
| Remainder assignment |
% = |
$ A % = 5 |
$ A is assigned to $ a by the remainder of 5. |
4. Comparison Operators
| Operation Type |
Operator |
Example |
Result |
| Less |
< |
$ A <$ B |
If the value of $ a is less than the value of $ B, true is returned. Otherwise, false is returned. |
| Greater |
> |
$ A> $ B |
If the value of $ a is greater than the value of $ B, true is returned. Otherwise, false is returned. |
| Less than or equal |
<= |
$ A <= $ B |
If the value of $ a is less than or equal to the value of $ B, true is returned. Otherwise, false is returned. |
| Greater than or equal |
> = |
$ A> = $ B |
If the value of $ a is greater than or equal to the value of $ B, true is returned. Otherwise, false is returned. |
| Equal |
= |
$ A = $ B |
If the value of $ a is equal to the value of $ B, true is returned. Otherwise, false is returned. |
| Full |
=== |
$ A ===$ B |
If the value of $ a is equal to the value of $ B and $ a is of the same type as $ B, true is returned. Otherwise, false is returned. |
| Not equal |
! = |
$! = $ B or $ a <> $ B |
If the value of $ a is equal to the value of $ B, false is returned. Otherwise, true is returned. |
| Incomplete |
! = |
$! ==$ B |
If $ a is equal to the value of $ B and $ a is of the same type as $ B, false is returned. Otherwise, true is returned. |