📅  最后修改于: 2020-10-21 05:05:39             🧑  作者: Mango
什么是操作员?可以使用表达式4 + 5等于9给出简单答案。这里的4和5称为操作数,而+称为运算符。 PHP语言支持以下类型的运算符。
让我们一一看一下所有运算符。
PHP语言支持以下算术运算运算符-
假设变量A持有10,变量B持有20,则-
Operator | Description | Example |
---|---|---|
+ | Adds two operands | A + B will give 30 |
– | Subtracts second operand from the first | A – B will give -10 |
* | Multiply both operands | A * B will give 200 |
/ | Divide numerator by de-numerator | B / A will give 2 |
% | Modulus Operator and remainder of after an integer division | B % A will give 0 |
++ | Increment operator, increases integer value by one | A++ will give 11 |
— | Decrement operator, decreases integer value by one | A– will give 9 |
有由PHP语言支持以下运算符
假设变量A持有10,变量B持有20,则-
Operator | Description | Example |
---|---|---|
== | Checks if the value of two operands are equal or not, if yes then condition becomes true. | (A == B) is not true. |
!= | Checks if the value of two operands are equal or not, if values are not equal then condition becomes true. | (A != B) is true. |
> | Checks if the value of left operand is greater than the value of right operand, if yes then condition becomes true. | (A > B) is not true. |
< | Checks if the value of left operand is less than the value of right operand, if yes then condition becomes true. | (A < B) is true. |
>= | Checks if the value of left operand is greater than or equal to the value of right operand, if yes then condition becomes true. | (A >= B) is not true. |
<= | Checks if the value of left operand is less than or equal to the value of right operand, if yes then condition becomes true. | (A <= B) is true. |
PHP语言支持以下逻辑运算符
假设变量A持有10,变量B持有20,则-
Operator | Description | Example |
---|---|---|
and | Called Logical AND operator. If both the operands are true then condition becomes true. | (A and B) is true. |
or | Called Logical OR Operator. If any of the two operands are non zero then condition becomes true. | (A or B) is true. |
&& | Called Logical AND operator. If both the operands are non zero then condition becomes true. | (A && B) is true. |
|| | Called Logical OR Operator. If any of the two operands are non zero then condition becomes true. | (A || B) is true. |
! | Called Logical NOT Operator. Use to reverses the logical state of its operand. If a condition is true then Logical NOT operator will make false. | !(A && B) is false. |
PHP语言支持以下赋值运算符-
Operator | Description | Example |
---|---|---|
= | Simple assignment operator, Assigns values from right side operands to left side operand | C = A + B will assign value of A + B into C |
+= | Add AND assignment operator, It adds right operand to the left operand and assign the result to left operand | C += A is equivalent to C = C + A |
-= | Subtract AND assignment operator, It subtracts right operand from the left operand and assign the result to left operand | C -= A is equivalent to C = C – A |
*= | Multiply AND assignment operator, It multiplies right operand with the left operand and assign the result to left operand | C *= A is equivalent to C = C * A |
/= | Divide AND assignment operator, It divides left operand with the right operand and assign the result to left operand | C /= A is equivalent to C = C / A |
%= | Modulus AND assignment operator, It takes modulus using two operands and assign the result to left operand | C %= A is equivalent to C = C % A |
还有一个叫运算符条件运算符。这首先对表达式的真值或假值求值,然后根据求值结果执行两个给定语句之一。条件运算符具有以下语法-
Operator | Description | Example |
---|---|---|
? : | Conditional Expression | If Condition is true ? Then value X : Otherwise value Y |
我们上面讨论过的所有运算符都可以分为以下几类:
一元前缀运算符,位于单个操作数之前。
二进制运算符,它接受两个操作数并执行各种算术和逻辑运算。
条件运算符(三元运算符),它接受三个操作数,并根据第一个表达式的求值来计算第二个或第三个表达式。
赋值运算符,将一个值赋给一个变量。
运算符优先级确定表达式中术语的分组。这会影响表达式的求值方式。某些运算符具有更高的优先级;例如,乘法运算符的优先级比加法运算符-
例如x = 7 + 3 * 2;在这里给x分配了13,而不是20,因为运算符*的优先级比+高,因此它首先与3 * 2相乘,然后加到7。
在此,优先级最高的运算符出现在表格的顶部,而优先级最低的运算符出现在表格的底部。在表达式中,优先级较高的运算符将首先被评估。
Category | Operator | Associativity |
---|---|---|
Unary | ! ++ — | Right to left |
Multiplicative | * / % | Left to right |
Additive | + – | Left to right |
Relational | < <= > >= | Left to right |
Equality | == != | Left to right |
Logical AND | && | Left to right |
Logical OR | || | Left to right |
Conditional | ?: | Right to left |
Assignment | = += -= *= /= %= | Right to left |