php –运算子

[rps-include post=6522]

[rps-include post = 6522]

We have almost learned the variables and related types. But just variables do not enough to create useful applications. We generally need to operate over these variables and data. Operators are a way to operate on variable and data. For example if we need to sum two integer variables we should use sum operator. Let’s start looking available operators. We groups operators with the following group names.

我们几乎了解了变量和相关类型。 但是仅仅变量不足以创建有用的应用程序。 我们通常需要处理这些变量和数据。 运算符是对变量和数据进行运算的一种方式。 例如,如果我们需要对两个整数变量求和,则应使用求和运算符。 让我们开始寻找可用的运算符。 我们使用以下群组名称对运营商进行分组。

  • Arithmetic Operators

    算术运算符
  • Assignment Operators

    赋值运算符
  • Comparison Operators

    比较运算符
  • Increment Operators

    增量运算符
  • Logical Operators

    逻辑运算符
  • String Operators

    字符串运算符
  • Array Operators

    数组运算符

算术运算符(Arithmetic Operators)

Arithmetic or Mathematical operations can be done with arithmetic operators. Here list of arithmetic operators.

算术或数学运算可以使用算术运算符完成。 这里是算术运算符列表。

+求和运算符 (+ Sum Operator)

Sum operator is used to sum two variables or values. In this example $result will be 7.

求和运算符用于对两个变量或值求和。 在此示例中, $result将为7。

$x = 5; $y = 2;

$result = $x + $y;

–减法运算符 (– Subtract Operator)

Subtract operator is used to subtract two variables or values. In this example $result will be 3 .

减法运算符用于减去两个变量或值。 在此示例中, $result将为3。

$x = 5; $y = 2;

$result = $x - $y;

*乘运算符 (* Multiply Operator)

Multiply operator is used to multiply two variables or values. In this example $result will be 10 .

乘法运算符用于将两个变量或值相乘。 在此示例中, $result将为10。

$x = 5; $y = 2;

$result = $x * $y;

/除法运算符 (/ Divide Operator)

Divide operator is used to divide two variables or values. In this example $result will be 2.5 . The $result type will be a float number.

除法运算符用于除以两个变量或值。 在此示例中, $result将为2.5。 $result类型将是一个浮点数。

$x = 5; $y = 2;

$result = $x / $y;

%模块运算符 (% Module Operator)

Module operator is used to get remainder two variables or values. In this example $result will be 1 .

模块运算符用于获取剩余的两个变量或值。 在此示例中, $result将为1。

$x = 5; $y = 2;

$result = $x % $y;

赋值运算符 (Assignment Operators)

Assignment operators generally provide same functionality of Arithmetic operators with a bit different way. Assignment operators uses first value or variable to provide value and store result.

赋值运算符通常以不同的方式提供算术运算符的相同功能。 赋值运算符使用第一个值或变量来提供值并存储结果。

=分配 (= Assign)

Assignment will set variables value to the given.

赋值会将变量值设置为给定值。

$a = "This is string";

$b = 5;

+ =添加并分配 (+= Add and Assign)

Add and assign operator will add left and right variables or values and set the result to the left variable. In this example $x will be 7 .

“添加并分配”运算符将添加左右变量或值,并将结果设置为左变量。 在此示例中, $x将为7。

$x = 5; $y = 2;

$x += $y;

-=减去并分配 (-= Subtract and Assign)

Subtract and assign operator will subtract left and right variables or values and set the result to the left variable. In this example $x will be 3 .

减法和赋值运算符将减去左右变量或值,并将结果设置为左变量。 在此示例中, $x将为3。

$x = 5; $y = 2;

$x -= $y;

* =乘和分配 (*= Multiply and Assign)

Multiply and assign operator will multiply left and right variables or values and set the result to the left variable. In this example $x will be 10 .

乘和赋运算符将乘以左变量和右变量或值,并将结果设置为左变量。 在此示例中, $x将为10。

$x = 5; $y = 2;

$x *= $y;

/ =划分和分配 (/= Divide and Assign)

Divide and assign operator will divide left and right variables or values and set the result to the left variable. In this example $x will be 7 .

除法和赋值运算符将对左右变量或值进行除法,并将结果设置为左变量。 在此示例中, $x将为7。

$x = 5; $y = 2;

$x /= $y;

%=划分并分配模量 (%= Divide and Assign Modulus)

Divide and assign operator will divide left and right variables or values and set the remainder to the left variable. In this example $x will be 7 .

除法和赋值运算符将对左变量和右变量或值进行除法,并将余数设置为左变量。 在此示例中, $x将为7。

$x = 5; $y = 2;

$x %= $y;

比较运算符 (Comparison Operators)

Comparison operators generally used to compare two values or variables. These operators generally used to return some boolean result like true and false  and used generally with conditional keywords like if .

比较运算符通常用于比较两个值或变量。 这些运算符通常用于返回布尔结果(如truefalse并且通常与条件关键字(如if

==等于 (== Equal)

Equal is used simply check whether given two values or variables are equal. If they are same this will return true if not this will return false.

等于仅用于检查给定的两个值或变量是否相等。 如果它们相同,则返回true,否则返回false。

$x = 5;
$y = 10;
$z = "5";

($x == $y); //Returns false
($x == $z); //Returns true

===相同(=== Identical)

Equal is used simply check whether given two values or variables are identical. If they are identical this will return true if not this will return false.

等于仅用于检查给定的两个值或变量是否相同。 如果它们相同,则返回true,否则返回false。

$x = 5;
$y = 10;
$z = "5";

($x === $y); //Returns false
($x === $z); //Returns false

!=或<>不等于 (!= or <> Not Equal)

Not equal is used simply check whether given two values or variables are not equal. If they are same this will return false if not this will return true.

不相等仅用于检查给定的两个值或变量是否不相等。 如果它们相同,则将返回false;否则,将返回true。

$x = 5;
$y = 10;
$z = "5";

($x <> $y); //Returns false
($x != $z); //Returns true

!==不相同 (!== Not Identical)

Not identical is used simply check whether given two values or variables are identical. If they are identical this will return false if not this will return true.

使用不相同只是检查给定的两个值或变量是否相同。 如果它们相同,则返回false,否则返回true。

$x = 5;
$y = 10;
$z = "5";

($x !== $y); //Returns true
($x !== $z); //Returns true

<少于 (< Less Than)

Less than is used simply check whether first variables or values is lesser than second one.  If first variables is lesser than second one this will return true if not false.

小于使用只需检查第一个变量或值是否小于第二个。 如果第一个变量小于第二个变量,则如果不为假,则返回true。

$x = 5;
$y = 10;

($x < $y); //Returns true
($y < $x); //Returns false

>大于 (> Greater Than)

Greater than is used simply check whether first variables or values is greater than second one.  If first variables is greater than second one this will return true if not false.

大于使用仅检查第一个变量或值是否大于第二个即可。 如果第一个变量大于第二个变量,则如果不为假,则返回true。

$x = 5;
$y = 10;

($x > $y); //Returns false
($y > $x); //Returns true

> =大于或等于 (>= Greater Than Or Equal To)

Greater than  or equal to is used simply check whether first variables or values is greater or equal to than second one.  If first variables is greater or equal to than second one this will return true if not false.

大于或等于仅用于检查第一个变量或值是否大于或等于第二个。 如果第一个变量大于或等于第二个变量,则为假,否则返回true。

$x = 5;
$y = 10;

($x >= $y); //Returns false
($y >= $x); //Returns true

<=小于或等于 (<= Less Than or Equal To)

Less than  or equal to is used simply check whether first variables or values is less or equal to than second one.  If first variables is less or equal to than second one this will return true if not false.

小于或等于仅用于检查第一个变量或值是否小于或等于第二个。 如果第一个变量小于或等于第二个变量,则如果不为false,则返回true。

$x = 5;
$y = 10;

($x <= $y); //Returns true
($y <= $x); //Returns false

增量运算符 (Increment Operators)

Increment operators generally used as mathematical operations.

增量运算符通常用作数学运算。

LEARN MORE  Python Type Function with Examples
了解更多带有示例的Python类型函数

++ $ x预增(++$x Pre-increment)

We can use pre-increment operator to increment single step the variable value before using this variable. In this example $x before  value is 6 and after value is 6 .

在使用此变量之前,我们可以使用pre-increment运算符单步递增变量值。 在此示例中, $x在value之前为6,在value为6之后。

$x=5;

echo $x; //Output is 5

echo ++$x;//Output is 6

echo $x; //Output 6

$ x ++后递增 ($x++ Post-increment)

We can use post-increment operator to increment single step the variable value after using this variable. In this example $x before  value is 5 and after value is 6 .

使用此变量后,我们可以使用后递增运算符来单步递增变量值。 在此示例中, $x在value之前为5,在value为6之后。

$x=5;

echo $x; //Output is 5

echo $x++;//Output is 5

echo $x; //Output 6

– $ x递减 (–$x Pre-decrement)

We can use pre-decrement operator to decrement single step the variable value before using this variable. In this example $x before  value is 4 and after value is 4 .

在使用此变量之前,我们可以使用减前运算符将变量值减一。 在此示例中, $x在value之前为4,在value为4之后。

$x=5;

echo $x; //Output is 5

echo --$x;//Output is 4

echo $x; //Output 4

$ x –递减后 ($x– Post-decrement)

We can use post-decrement operator to decrement single step the variable value after using this variable. In this example $x before  value is 5 and after value is 64.

使用此变量后,我们可以使用减后运算符将变量值减一。 在此示例中, $x前值为5,后值为64。

$x=5;

echo $x; //Output is 5

echo $x--;//Output is 5

echo $x; //Output 4

逻辑运算符 (Logical Operators)

Local operators generally used with boolean values and variables.

局部运算符通常与布尔值和变量一起使用。

和,&&运算符 (and , && Operator)

and operator is used like below. If two values or variables are true this will return true if not it will return false. && can be used as and operator too.

and运算符的用法如下。 如果两个值或变量为true,则为true,否则为false。 &&也可以用作and运算符。

$x = true; $y =true; $z = false

($x && $y); //Returns true

($x and $z); //Returns false

或|| 操作员 (or , || Operator)

or operator is used like below. If two values or variables are false this will return false if not it will return true. || can be used as and operator too.

or运算符的用法如下。 如果两个值或变量为false,则返回false;否则为true。 || 也可以用作和运算符。

$x = true; $y =true; $z = false

($x || $y); //Returns true

($x or $z); //Returns true

异或运算符 (xor  Operator)

xor operator is used like below. If two values or variables are same boolean value this will return true if not will return false.

xor运算符的用法如下。 如果两个值或变量是相同的布尔值,则返回true,否则返回false。

$x = true; $y =true; $z = false

($x xor $y); //Returns true

($x xor $z); //Returns false

($z xor $z); //Returns true

! 不是操作员 (! Not Operator)

Not operator simply return the reverse boolean value of the given variable or value. If given variable of value is true result will be false. If the given value is false result will be true.

不是运算符,只是返回给定变量或值的反向布尔值。 如果给定的value变量为true,则结果为false。 如果给定值为false,则结果为true。

$x = true; $y =true; $z = false

!$x; //Returns false
!$z; //Returns true

字符串运算符 (String Operators)

String operators mainly used to concatenate given string variables or values.

字符串运算符主要用于连接给定的字符串变量或值。

LEARN MORE  Winmerge Tutorial With Examples To Visual Patch, Diff, Merge
了解更多Winmerge教程,包括可视化补丁,差异和合并示例

。 连接 (. Concatenate)

We can concatenate two string variable of value with . like below. In this example we will concatenate two string variable named $a and $b and result will be assigned to the $result .

我们可以用连接两个值字符串变量. 像下面一样。 在此示例中,我们将串联两个名为$a$b字符串变量,并将结果分配给$result

$a="This is";

$b="string";

$result = $a . $b; //Result will be "This is string"

。=追加 (.= Append)

We can concatenate two string variable of value with . like below. In this example we will concatenate two string variable named $a and $b and result will be assigned to the $a .

我们可以用连接两个值字符串变量. 像下面一样。 在此示例中,我们将串联两个名为$a$b字符串变量,并将结果分配给$a

$a="This is";

$b="string";

$a .= $b; //$a will be "This is string"

[rps-include post=6522]

[rps-include post = 6522]

翻译自: https://www.poftut.com/php-operators/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值