Summarize operators in PHP
Sep 5, 2020
PHP
beginner
operator
programming study diary
#Programming study diary
September 5, 2020
I thought I knew most of the operators, but there were some operators I didn’t know, such as <>
and . =
, So I’ll take this opportunity to put them all together.
#Arithmetic operator
An operator that performs simple addition, subtraction, multiplication, and division such as addition, subtraction, multiplication, and division. Addition is used not only for numbers but also for concatenating strings.
Addition is indicated by add
, subtraction is indicated by sub
, multiplication is indicated by mul
, quotient of division is indicated by div
, and remainder of division is indicated by mod
.
Operator | Meaning | Description |
---|---|---|
+ |
Addition | Addition of two or more values |
- |
Subtract | Subtract two or more values |
* |
Multiply | Multiply two or more values |
/ |
Division | Divide two or more values |
% |
Residue | Find too many when divided |
<? php
$ add = 17 + 5;
echo "17 + 5 = $ add <br> \ n";
$ sub = 17-5;
echo "17-5 = $ sub <br> \ n";
$ mul = 17 * 5;
echo "17 * 5 = $ mul <br> \ n";
$ div = 17/5;
echo "17/5 = $ div <br> \ n";
$ mod = 17% 5;
echo "17% 5 = $ mod <br> \ n";
?>
17 + 5 = 22
17 --5 = 12
17 * 5 = 85
17/5 = 3.4
17% 5 = 2
#Comparison operator
The equivalent operator of the comparison operator is summarized in This article (It seems that the meaning of == and === is different in PHP).
Comparing two values under certain conditions, if the conditions are met, true
is returned, otherwise false
is returned. The comparison operator indicates a certain condition.
Operator | Meaning | Example | Description |
---|---|---|---|
== |
Equal | $ a == 10 | Two values are equal (variable a is equal to 10) |
! = |
Not equal | $ a! = 10 | Two values are not equal (variable a is not equal to 10) |
<> |
Not equal | $ a <> 10 | Two values are not equal (variable a is not equal to 10) |
< |
Small | $ a <10 | The value on the left side is smaller than the right side (variable a is less than 10) |
> |
Large | $ a> 10 | The value on the left side is greater than the right side (variable a is greater than 10) |
<= |
Same or less (less than or equal to) | $ a <= 10 | Value on the left side is less than or equal to the right side (variable a is less than or equal to 10) |
> = |
Same or greater than (greater than or equal to) | $ a> = 10 | Value on the left side is greater than or equal to the right side (variable a is greater than or equal to 10) |
<? php
$ a = 10;
if ($ a == 10) echo "The value of \ $ a is equal to 10 <br> \ n";
if ($ a <10) echo "The value of \ $ a is greater than 10 <br> \ n";
if ($ a> 20) echo "The value of \ $ a is less than 20 <br> \ n";
if ($ a <= 20) echo "The value of \ $ a is greater than or equal to 20 <br> \ n";
if ($ a <= 20) echo "The value of \ $ a is less than or equal to 20 <br> \ n";
if ($ a! = 20) echo "The value of \ $ a is not equal to 20 <br> \ n";
if ($ a <> 20) echo "The value of \ $ a is not equal to 20";
?>
The value of $ a is equal to 10
The value of $ a is less than 20
The value of $ a is less than or equal to 20
The value of $ a is not equal to 20
The value of $ a is not equal to 20
#Assignment operator
The =
used when assigning a value to a variable is called an assignment operator.
Operator | Example | Meaning |
---|---|---|
= |
a = b | Substitute b for a |
+ = |
a + = b | a = a + same as b |
-= |
a-= b | a = a –same as –b |
* = |
a * = b | a = a * Same as b |
/ = |
a / = b | a = same as a / b |
% = |
a% = b | a = a Same as% b |
& = |
a & = b | a = a = same as & b |
^ = |
a ^ = b | a = a ^ same as b |
<< = |
a « = b | a = a « same as b |
>> = |
a » = b | a = a » Same as b |
> >> = |
a »> = b | a = a »> Same as b |
. = |
A. = b | ab (concatenate the value of b to a) |
(Same as | =
, a | = b, a = a | b)
#Logical operator Operators are used in some conditional expressions to create certain conditions. The operator described in the conditional expression is called a logical operator. The value returned by the logical operator is true or false.
Operator | Meaning | Description |
---|---|---|
&& |
Logical product | When two values hold |
and | AND | When two values hold |
or | OR | When one or both of the two values hold |
xor | Exclusive OR | When one of the two values holds and neither holds |
! | Negation | When the value does not hold |
(||
, OR, when one or both of the two values hold)
#References Operator Assignment operator