Operator is a symbol used to perform certain operations on arrays and variables. Control structures If we want to effectively respond to user input, the code needs to have the ability to judge. The structure that allows the program to make judgments is called a condition
Operator
Operator is used to perform certain operations on arrays and variables symbol.
1. Arithmetic operators
Operators |
Name |
Example |
##+ |
Add |
##$a+$b |
##-
| minus
| $ a-$b
|
#*
##multiply |
$a*$b |
##/ |
except |
##$a/$b |
%
|
##remainder
|
$a %$b
|
##2. Compound assignment operator
|
Operator
Usage method
| is equivalent to
|
+= |
##$a+=$b
$a=$a+$b |
| ##-= | $a-=$b
##$a=$a-$b |
*= |
$a*=$b |
$a=$a*$b
|
##/= |
$a/=$b |
$a=$a/$b | ##%= |
##$a%=$b |
$a=$a%$b
|
##.=
|
$a.=$b
|
$a=$a.$b
Prefix increment and decrement and postfix Increment and decrement operator :
$a=++$b;
$a=$b++; $a=--$b;
$a=$b--;
3, Comparison operator
Operator |
Name |
How to use |
= = |
equals |
$a= =$b |
##= = = |
hengheng |
##$a= = =$b
|
!=
| ##Not equal
| $a!=$b
|
##!= =
Not Identity |
##$a!= =$b |
| <>
Not equal |
## $a<>$b |
#< | Less than | $a<$b | #> |
is greater than
| ##$a>$b
|
##<=
| less than or equal to $a<=$b |
| #>=
| Greater than or equal
##$a>=$b |
##
Note: Identity means that only if the operands on both sides are equal and the data types are also equivalent, true will be returned;
For example: 0= = "0" This returns is true because the operands are equal
0== ="0" This returns false because the data types are different
4, Logical operators
Operator |
Usage |
How to use |
Instructions |
! |
##non |
##!$b
|
If $b is false, returns true;Otherwise the opposite
|
##&&
## with |
##$a&&$b |
##if$a | and $b are both true, then the result is true; otherwise false##|| |
or |
##$a||$b
| ##if Either one of $a and $b |
is true or both are true## When #, the result is true; otherwise, it is false##and ##与 |
$a and $b |
## is the same as && , but its | priority is lower
|
##or##or |
$a or $b
| is the same as || , but its priority is higher Low
|
Operator"and"和"or" is better than &&and || has a lower priority.
5. Ternary operator
Condition ? value if true : value if false
Example: ($grade>=50 ? "Passed " : "Failed")
6. Error suppression operator:
$a=@(57/0);
The divisor cannot be 0 will cause an error, so add @ to avoid error warnings.
7. Array Operator
##Operator |
How to use |
How to use |
Instructions |
##+
| ##United
| !$b
| Returns a message containing $a and $ Array of all elements in b
|
##= =
Equivalent |
##$a&&$b |
##If $a | and
$b have the same elements, return true = = = |
##hengheng |
$a||$b |
##If $a and
| $b With the same elements and the same order, return true##!=
|
Non-equivalent
##$a and $b |
If | $a and $b | are not equivalent, return
true#<> ##Not equivalent |
|
If $a and
| $b is not equivalent, returns | true
|
!= = |
##non-identity |
$a or $b |
if $a and $b is not identical, returns true |
Priority and associativity of operators:
Generally speaking, operators have a set of priorities, which is the order in which they are executed.
Operators are also associative, that is, the execution order of operators with the same priority. This order is usually left to right, right to left or irrelevant.
The table of operator precedence is given below. The top operator has the lowest priority, and the priority increases from top to bottom in the table.
Operator precedence
Associativity |
Operator |
##left |
, |
left |
##Or
|
left
|
Xor
|
left
|
And
|
right
| ##Print
|
##left
##= += -= *= /= .= %= &= |= ^= ~= <<= >>= |
| Left
? : |
| Left
|| |
##left |
&& |
left |
##| |
left |
^ |
Left |
& |
Not relevant |
##= = != = = = = != =
|
Irrelevant |
#<<= >>=
|
left |
##<< >>
|
left
|
+ - .
|
Left
|
* / %
|
right
|
! ~ ++ -- (int)(double)(string)(array)(object) @
|
right
|
[] |
Not relevant |
##New |
irrelevant |
() |
In order to avoid priority confusion, you can use parentheses to avoid priority.
|
|
|
The above is the detailed content of Tutorial on the basics of operators in php. For more information, please follow other related articles on the PHP Chinese website!