PHP运算符

时间:2020-02-23 14:42:02  来源:igfitidea点击:

在本教程中,我们将学习PHP中使用的不同类型的操作符。

  • 算术运算符

  • 赋值运算符

  • 位运算符

  • 比较运算符

  • 逻辑运算符

  • 递增/递减运算符

算术运算符

这些运算符是加法、减法、乘法、除法和模。

运算符示例
+(加法)1+2=3
-(减法)2-1=1
*(乘法)4*5=20
/(除法)4/2=2
%(模数)5%2=1(我们得到余数)

赋值运算符

为了给任何变量赋值,我们使用赋值运算符。 $name = "theitroad"; //we have assigned a string value to a variable.我们也可以将赋值运算符与+,等其它运算符结合起来,以缩短某些表达式。

在下面的表达式中,我们将10赋给变量x,20赋给变量y。
然后,我们将x和y相加,并将最终结果保存在变量x中。

$x = 10;
$y = 20;
$x = $x + $y;

上述加法表达式可以按以下方式简化。

$x = 10;
$y = 20;
$x += $y;	//this means $x = $x + $y

下面是组合赋值运算符。

组合赋值运算符展开形式
$x+=$y$x=$x+$y
$x-=$y$x=$x-$y
$x*=$y$x=$x*$y
$x/=$y$x=$x/$y
$x%=$y$x=$x%$y

位运算符

位运算符用于处理整数值的各个位。
以下是按位运算符的列表。

运算符说明示例
&(与)如果两个位都是1,则输出为11010&1100=1000
|(或)如果任何一位为1,则输出为11010
~(取反)将1更改为0,将0更改为1~1011=0100
^(异或)如果只有一个位设置为1,而不是两个位都设置为1,则输出为11010^1100=0110
<<(左移)将位左移N位000011<<2=001100
>>(右移)将位右移N位001100>>2=000011

比较运算符

比较运算符用于比较两个操作数,结果为布尔形式,即true或者false。

运算符说明示例
==(equal)如果两边相等,则返回true$x==$y
如果$x等于$y,则返回true
==(相同)如果两边相等且类型相同,则返回true$x===$y
如果$x等于$y且类型相同,则返回true
!=(不等于)如果一个不等于另一个,则返回true$x!=$y
如果$x不等于$y,则为真
!==(不完全相同)如果其中一个不等于另一个并且类型也不相同,则返回true$x!==$y
如果$x不等于$y并且它们也不是同一类型,则为true
<(小于)如果左侧小于右侧,则返回true$x<$y
如果$x小于$y,则返回true
>(大于)如果左侧大于右侧,则返回true$x>$y
如果$x大于$y,则返回true
<=(小于或者等于)如果左侧小于或者等于右侧,则返回true$x<=$y
如果$x小于或者等于$y,则返回true
>=(大于或者等于)如果左侧大于或者等于右侧,则返回true$x>=$y
如果$x小于或者等于$y,则返回true

逻辑运算符

逻辑运算符用于处理布尔值,即true或者false。
首先让我们看看PHP为一些表达式指定的布尔值。

以下为布尔值 false.

  • 文字值false

  • 空字符串""

  • 零字符串"0"

  • 整数值0

  • 浮动值0.0

  • 零元素数组

  • 空值

所有其他值计算为 true.

运算符示例说明
&&$x&&$y如果$x和$y的计算结果都为true,则为true,否则结果为false
||$x || $y如果$x或者$y计算结果为true,则为true,否则结果为false
!!$x如果$x计算为false,则为true,反之亦然
and$x and $y如果$x和$y的计算结果都为true,则为true,否则结果为false
or$x or $y如果$x或者$y计算为true,则为true,否则结果为false
xor$x xor$y如果$x或者$y且不是都计算为true,则为true,否则结果为false

递增/递减运算符

增量运算符用于将变量的值增加1.
使用减量运算符将值减少1.

++是递增运算符,-是递减运算符。

$x = 10;		//x set to 10
echo $x;	//output 10
//now increase the value of x by 1
$x = $x + 1;
echo $x 	//output 11

现在使用增量运算符。

$x = 10;		//x set to 10
echo $x;	//output 10
//now increase the value of x by 1
$x++;
echo $x 	//output 11

减量运算

$x = 10;		//x set to 10
echo $x;	//output 10
//now decrease the value of x by 1
$x = $x - 1;
echo $x 	//output 9

现在使用减量运算符。

$x = 10;		//x set to 10
echo $x;	//output 10
//now decrease the value of x by 1
$x--;
echo $x 	//output 9

在表达式中使用递增运算符和递减运算符时要注意的要点。

++$x; //adds one to $x and then returns the result
$x++; //returns value of $x and then adds one to $x
–-$x; //subtracts one from $x and then returns the result
$x–-; //returns value of $x and then subtracts one from $x

让我们检查一些例子。

预增量(++在变量之前)

//set x to 10
$x = 10;
echo $x;	//output 10
//now assign value of x to y using pre-increment
//this is first increment x by 1 and then assign the new value to y
$y = ++$x;
echo $y;	//output 11
echo $x;	//output 11

后置增量(++在变量之后)

//set x to 10
$x = 10;
echo $x;	//output 10
//now assign value of x to y using post-increment
//first use value of x then increment by 1
$y = $x++;
echo $y;	//output 10
echo $x;	//output 11

预减量(-变量前)

//set x to 10
$x = 10;
echo $x;	//output 10
//now assign value of x to y using pre-decrement
//this will first decrease value of x by 1 then assign to y
$y = --$x;
echo $y;	//output 9
echo $x;	//output 9

后减量(-变量后)

//set x to 10
$x = 10;
echo $x;	//output 10
//now assign value of x to y using post-decrement
//this will first use the value of x then decrease by 1
$y = $x--;
echo $y;	//output 10
echo $x;	//output 9