📜  PHP之operators

📅  最后修改于: 2020-09-28 02:10:11             🧑  作者: Mango

PHP运算符

PHP运算符是一个符号,即用于对操作数执行操作。简而言之,运算符用于对变量或值执行运算。例如:

$num=10+20;//+ is the operator and 10,20 are operands

在上面的示例中,+是二进制+运算符,10和20是操作数,$num是变量。

PHP运算符可以按以下形式分类:

  • 算术运算符
  • 赋值运算符
  • 按位运算符
  • 比较运算符
  • 递增/递减运算符
  • 逻辑运算符
  • 字符串运算符
  • 数组运算符
  • 类型运算符
  • 执行运算符
  • 错误控制运算符

我们还可以代表操作数对运算符进行分类。它们可以分为3种形式:

  • 一元运算符:适用于++,-等单个操作数。
  • 二进制运算符:对两个操作数起作用,例如二进制+,-,*,/等。
  • 三元运算符:对三个运算符(例如“?:”)进行运算。

算术运算符

PHP算术运算符用于执行常用的算术运算,例如对数值进行加法,减法等。

Operator Name Example Explanation
+ Addition $a + $b Sum of operands
Subtraction $a – $b Difference of operands
* Multiplication $a * $b Product of operands
/ Division $a / $b Quotient of operands
% Modulus $a % $b Remainder of operands
** Exponentiation $a ** $b $a raised to the power $b

指数(**)运算符已在PHP5.6中引入。

赋值运算符

赋值运算符用于将值赋给不同的变量。基本赋值运算符为“=”。

Operator Name Example Explanation
= Assign $a = $b The value of right operand is assigned to the left operand.
+= Add then Assign $a += $b Addition same as $a = $a + $b
-= Subtract then Assign $a -= $b Subtraction same as $a = $a – $b
*= Multiply then Assign $a *= $b Multiplication same as $a = $a * $b
/= Divide then Assign
(quotient)
$a /= $b Find quotient same as $a = $a / $b
%= Divide then Assign
(remainder)
$a %= $b Find remainder same as $a = $a % $b

按位运算符

按位运算符用于对操作数执行位级运算。这些运算符允许对整数内的特定位进行评估和处理。

Operator Name Example Explanation
& And $a & $b Bits that are 1 in both $a and $b are set to 1, otherwise 0.
| Or (Inclusive or) $a | $b Bits that are 1 in either $a or $b are set to 1
^ Xor (Exclusive or) $a ^ $b Bits that are 1 in either $a or $b are set to 0.
~ Not ~$a Bits that are 1 set to 0 and bits that are 0 are set to 1
<< Shift left $a << $b Left shift the bits of operand $a $b steps
>> Shift right $a >> $b Right shift the bits of $a operand by $b number of places

比较运算符

比较运算符允许比较两个值,例如number或字符串。下面给出了比较运算符的列表:

Operator Name Example Explanation
== Equal $a == $b Return TRUE if $a is equal to $b
=== Identical $a === $b Return TRUE if $a is equal to $b, and they are of same data type
!== Not identical $a !== $b Return TRUE if $a is not equal to $b, and they are not of same data type
!= Not equal $a != $b Return TRUE if $a is not equal to $b
<> Not equal $a <> $b Return TRUE if $a is not equal to $b
< Less than $a < $b Return TRUE if $a is less than $b
> Greater than $a > $b Return TRUE if $a is greater than $b
<= Less than or equal to $a <= $b Return TRUE if $a is less than or equal $b
>= Greater than or equal to $a >= $b Return TRUE if $a is greater than or equal $b
<=> Spaceship $a <=>$b Return -1 if $a is less than $b
Return 0 if $a is equal $b
Return 1 if $a is greater than $b

递增/递减运算符

递增和递减运算符用于增加和减少变量的值。

Operator Name Example Explanation
++ Increment ++$a Increment the value of $a by one, then return $a
$a++ Return $a, then increment the value of $a by one
decrement –$a Decrement the value of $a by one, then return $a
$a– Return $a, then decrement the value of $a by one

逻辑运算符

逻辑运算符用于对操作数执行位级运算。这些运算符允许对整数内的特定位进行评估和处理。

Operator Name Example Explanation
and And $a and $b Return TRUE if both $a and $b are true
Or Or $a or $b Return TRUE if either $a or $b is true
xor Xor $a xor $b Return TRUE if either $ or $b is true but not both
! Not ! $a Return TRUE if $a is not true
&& And $a && $b Return TRUE if either $a and $b are true
|| Or $a || $b Return TRUE if either $a or $b is true

字符串运算符

字符串运算符用于对字符串执行操作。PHP中有两个字符串运算符,如下所示:

Operator Name Example Explanation
. Concatenation $a . $b Concatenate both $a and $b
.= Concatenation and Assignment $a .= $b First concatenate $a and $b, then assign the concatenated string to $a, e.g. $a = $a . $b

数组运算符

如果是数组,则使用数组运算符。基本上,这些运算符用于比较数组的值。

Operator Name Example Explanation
+ Union $a + $y Union of $a and $b
== Equality $a == $b Return TRUE if $a and $b have same key/value pair
!= Inequality $a != $b Return TRUE if $a is not equal to $b
=== Identity $a === $b Return TRUE if $a and $b have same key/value pair of same type in same order
!== Non-Identity $a !== $b Return TRUE if $a is not identical to $b
<> Inequality $a <> $b Return TRUE if $a is not equal to $b

类型运算符

类型运算符instanceof用于确定对象,其父级及其派生类是否相同。基本上,此运算符确定对象属于哪个特定类。它用于面向对象的编程。

";
var_dump($charu instanceof Developer);           //It will return true.
var_dump($charu instanceof Programmer);       //It will return false.
?>

输出:

Charu is a developer.
bool(true) bool(false)

执行运算符

PHP具有执行运算符反引号(“)。PHP将反引号的内容作为shell命令执行。执行运算符和shell_exec()给出相同的结果。

Operator Name Example Explanation
backticks echo `dir`; Execute the shell command and return the result.
Here, it will show the directories available in current folder.

注意:请注意反引号(“)不是单引号。

错误控制运算符

PHP具有一个错误控制运算符,即(@)符号。只要将它与表达式一起使用,该表达式可能生成的任何错误消息都将被忽略。

Operator Name Example Explanation
@ at @file (‘non_existent_file’) Intentional file error

PHP运算符优先级

让我们看看具有关联性的PHP运算符的优先级。

Operators Additional Information Associativity
clone new clone and new non-associative
[ array() left
** arithmetic right
++ — ~ (int) (float) (string) (array) (object) (bool) @ increment/decrement and types right
instanceof types non-associative
! logical (negation) right
* / % arithmetic left
+ – . arithmetic and string concatenation left
<< >> bitwise (shift) left
< <= > >= comparison non-associative
== != === !== <> comparison non-associative
& bitwise AND left
^ bitwise XOR left
| bitwise OR left
&& logical AND left
|| logical OR left
?: ternary left
= += -= *= **= /= .= %= &= |= ^= <<= >>= => assignment right
and logical left
xor logical left
or logical left
, many uses (comma) left