PHP运算符
在本文中,我们将了解如何使用PHP中的运算符,以及各种可用的运算符,并通过示例了解它们的实现。
运算符用于对某些值执行操作。换句话说,我们可以将运算符描述为接受一些值,对它们执行一些操作并给出结果的东西。例如,这个表达式 '+' 中的“1 + 2 = 3”是一个运算符。它需要两个值 1 和 2,对它们执行加法运算得到 3。
就像任何其他编程语言一样, PHP也支持各种类型的运算,如算术运算(加法、减法等)、逻辑运算(AND、OR 等)、递增/递减运算等。因此, PHP为我们提供了许多运算符来对各种操作数或变量或值执行此类操作。这些运算符只不过是执行各种类型的操作所需的符号。下面给出了各种运算符组:
- 算术运算符
- 逻辑或关系运算符
- 比较运算符
- 条件或三元运算符
- 赋值运算符
- 宇宙飞船操作员(在PHP 7 中引入)
- 数组运算符
- 递增/递减运算符
- 字符串运算符
现在让我们详细了解这些运算符中的每一个。
算术运算符:
算术运算运算符用于执行简单的数学运算,如加法、减法、乘法等。下面是算术运算运算符列表以及它们在PHP中的语法和运算。 + Addition $x + $y Sum the operands – Subtraction $x – $y Differences the operands * Multiplication $x * $y Product of the operands / Division $x / $y The quotient of the operands ** Exponentiation $x ** $y $x raised to the power $y % Modulus $x % $y The remainder of the operandsOperator
Name
Syntax
Operation
注意:指数运算已在PHP 5.6 中引入。
例子:这个例子解释了PHP中的算术运算符。
PHP
PHP
PHP
$b) + "\n";
var_dump($a === $c) + "\n";
var_dump($a !== $c) + "\n";
var_dump($a < $b) + "\n";
var_dump($a > $b) + "\n";
var_dump($a <= $b) + "\n";
var_dump($a >= $b);
?>
PHP
0) ? 'The number is positive' : 'The number is negative';
?>
PHP
PHP
"Car", "l" => "Bike");
$y = array("a" => "Train", "b" => "Plane");
var_dump($x + $y);
var_dump($x == $y) + "\n";
var_dump($x != $y) + "\n";
var_dump($x <> $y) + "\n";
var_dump($x === $y) + "\n";
var_dump($x !== $y) + "\n";
?>
PHP
PHP
PHP
$y;
echo "\n";
echo $x <=> $z;
echo "\n";
echo $z <=> $y;
echo "\n";
// We can do the same for Strings
$x = "Ram";
$y = "Krishna";
echo $x <=> $y;
echo "\n";
echo $x <=> $y;
echo "\n";
echo $y <=> $x;
?>
输出:
33
25
116
7.25
1
逻辑或关系运算符:
这些基本上用于操作条件语句和表达式。条件语句是基于条件的。此外,条件可以满足也可以不满足,因此条件语句的结果可以是真或假。以下是PHP中的逻辑运算符及其语法和操作。Operator
Name
Syntax
Operation
and Logical AND $x and $y True if both the operands are true else false or Logical OR $x or $y True if either of the operands is true else false xor Logical XOR $x xor $y True if either of the operands is true and false if both are true && Logical AND $x && $y True if both the operands are true else false || Logical OR $x || $y True if either of the operands is true else false ! Logical NOT !$x True if $x is false
例子:这个例子描述了PHP中的逻辑 & 关系运算符。
PHP
输出:
and Success
or Success
xor Success
&& Success
|| Success
! Success
比较运算符:这些运算符用于比较两个元素并以布尔形式输出结果。以下是PHP中的运算符及其语法和操作。Operator Name Syntax Operation == Equal To $x == $y Returns True if both the operands are equal != Not Equal To $x != $y Returns True if both the operands are not equal <> Not Equal To $x <> $y Returns True if both the operands are unequal === Identical $x === $y Returns True if both the operands are equal and are of the same type !== Not Identical $x == $y Returns True if both the operands are unequal and are of different types < Less Than $x < $y Returns True if $x is less than $y > Greater Than $x > $y Returns True if $x is greater than $y <= Less Than or Equal To $x <= $y Returns True if $x is less than or equal to $y >= Greater Than or Equal To $x >= $y Returns True if $x is greater than or equal to $y
示例:此示例描述PHP中的运算符。
PHP
$b) + "\n";
var_dump($a === $c) + "\n";
var_dump($a !== $c) + "\n";
var_dump($a < $b) + "\n";
var_dump($a > $b) + "\n";
var_dump($a <= $b) + "\n";
var_dump($a >= $b);
?>
输出:
bool(true)
bool(true)
bool(true)
bool(false)
bool(true)
bool(false)
bool(true)
bool(false)
bool(true)
条件或三元运算符:
这些运算符用于比较两个值并同时获取任一结果,具体取决于结果是 TRUE 还是 FALSE。这些也用作if...else语句的简写符号,我们将在有关决策的文章中阅读。
语法:
$var = (condition)? value1 : value2;
在这里,条件将评估为真或假。如果条件评估为 True,则 value1 将分配给变量 $var,否则 value2 将分配给它。 ?: Ternary If the condition is true? then $x : or else $y. This means that if the condition is true then the left result of the colon is accepted otherwise the result is on right.Operator
Name
Operation
示例:此示例描述PHP中的条件或三元运算符。
PHP
0) ? 'The number is positive' : 'The number is negative';
?>
输出:
The number is negative
赋值运算符:这些运算符用于将值分配给不同的变量,有或没有中间操作。以下是PHP为操作提供的赋值运算符及其语法和操作。 = Assign $x = $y Operand on the left obtains the value of the operand on the right += Add then Assign $x += $y Simple Addition same as $x = $x + $y -= Subtract then Assign $x -= $y Simple subtraction same as $x = $x – $y *= Multiply then Assign $x *= $y Simple product same as $x = $x * $y /= Divide then Assign (quotient) $x /= $y Simple division same as $x = $x / $y %= $x %= $y Simple division same as $x = $x % $yOperator
Name
Syntax
Operation
Divide then Assign (remainder)
例子:这个例子描述了PHP中的赋值运算符。
PHP
输出:
75
300
60
600
20
0
数组运算符:这些运算符用于数组的情况。以下是PHP为数组操作提供的数组运算符及其语法和操作。 + Union $x + $y Union of both i.e., $x and $y == Equality $x == $y Returns true if both has same key-value pair != Inequality $x != $y Returns True if both are unequal === Identity $x === $y Returns True if both have the same key-value pair in the same order and of the same type !== Non-Identity $x !== $y Returns True if both are not identical to each other <> Inequality $x <> $y Returns True if both are unequalOperator
Name
Syntax
Operation
例子:这个例子描述了PHP中的数组操作。
PHP
"Car", "l" => "Bike");
$y = array("a" => "Train", "b" => "Plane");
var_dump($x + $y);
var_dump($x == $y) + "\n";
var_dump($x != $y) + "\n";
var_dump($x <> $y) + "\n";
var_dump($x === $y) + "\n";
var_dump($x !== $y) + "\n";
?>
输出:
array(4) {
["k"]=>
string(3) "Car"
["l"]=>
string(4) "Bike"
["a"]=>
string(5) "Train"
["b"]=>
string(5) "Plane"
}
bool(false)
bool(true)
bool(true)
bool(false)
bool(true)
递增/递减运算符:这些被称为一元运算运算符,因为它们在单个操作数上工作。这些用于增加或减少值。Operator
Name
Syntax
Operation
++ Pre-Increment ++$x First increments $x by one, then return $x — Pre-Decrement –$x First decrements $x by one, then return $x ++ Post-Increment $x++ First returns $x, then increment it by one — Post-Decrement $x– First returns $x, then decrement it by one
示例:此示例描述PHP中的增量/减量运算符。
PHP
输出:
3 First increments then prints
3
2 First prints then increments
3
1 First decrements then prints
1
2 First prints then decrements
1
字符串运算符:此运算符用于使用连接运算符('.') 连接 2 个或多个字符串。我们还可以使用连接赋值运算符('.=') 将右侧的参数附加到左侧的参数。Operator
Name
Syntax
Operation
. Concatenation $x.$y Concatenated $x and $y .= Concatenation and assignment $x.=$y First concatenates then assigns, same as $x = $x.$y
示例:此示例描述PHP中的字符串运算符。
PHP
输出:
GeeksforGeeks!!!
GeeksforGeeks!!!
宇宙飞船操作员:
PHP 7 引入了一种新的运算符,称为 spaceship运算符。宇宙飞船运算符或组合运算符由“<=>”表示。这些运算符用于比较值,但不是返回布尔结果,而是返回整数值。如果两个操作数相等,则返回 0。如果右操作数更大,则返回 -1。如果左操作数更大,则返回 1。下表详细显示了它的工作原理:Operator
Syntax
Operation
$x < $y $x <=> $y Identical to -1 (right is greater) $x > $y $x <=> $y Identical to 1 (left is greater) $x <= $y $x <=> $y Identical to -1 (right is greater) or identical to 0 (if both are equal) $x >= $y $x <=> $y Identical to 1 (if left is greater) or identical to 0 (if both are equal) $x == $y $x <=> $y Identical to 0 (both are equal) $x != $y $x <=> $y Not Identical to 0
例子:这个例子说明了在PHP中 spaceship运算符的使用。
PHP
$y;
echo "\n";
echo $x <=> $z;
echo "\n";
echo $z <=> $y;
echo "\n";
// We can do the same for Strings
$x = "Ram";
$y = "Krishna";
echo $x <=> $y;
echo "\n";
echo $x <=> $y;
echo "\n";
echo $y <=> $x;
?>
输出:
0
1
-1
1
1
-1