我应该在这里做什么?



=& 参考

参考任务操作员在PHP, =& 什么意思是“=&”和“&="操作员在PHP? 什么意思是“&=”和“=&”操作员在PHP?

















PHP 中的奇怪打印行为?



= 任命运营商

三种不同的平等


如何区分PHP平等(==双等)和身份(===三等)比较操作员?PHP!=和 ==操作员3个不同的平等类型和(严格)较大的/较小的比较在PHP


=== 比较运营商






比特币运营商


上一篇: 逻辑运营商












[ ] Arrays (自 PHP 5.4 以来简短的合成)


PHP 短标签是否可用?


二角形字符范围




# One-line shell 风格评论


NullSafe Operator 通话(自 PHP 8.0 以来)

PHP 中有“零安全操作员”吗?



当前回答

<=> 航天运营商

在 PHP 7 中添加

太空运营商 <=> 是 PHP 7 中添加的最新比较运营商。 它是一种与平等运营商相同的优先事项的非协作二进制运营商(==,!=, ===,!==)。 该运营商允许左手和右手运营商之间的简单三路比较。

运营商以一个完整的表达结果:

0 如果两位操作员均等 低于 0 如果左手操作员低于右手操作员 大于 0 如果左手操作员高于右手操作员

吉。

1 <=> 1; // 0
1 <=> 2; // -1
2 <=> 1; // 1

使用这个运营商的好实用应用将是比较类型的呼叫回归,预计将基于两个值之间的三路比较,返回零、负或积极的整体。

在PHP 7之前,你会写...

$arr = [4,2,1,3];

usort($arr, function ($a, $b) {
    if ($a < $b) {
        return -1;
    } elseif ($a > $b) {
        return 1;
    } else {
        return 0;
    }
});

因为 PHP 7 你可以写...

$arr = [4,2,1,3];

usort($arr, function ($a, $b) {
    return $a <=> $b;
    // return $b <=> $a; // for reversing order
});

其他回答

增加 / 减少运营商

+ 增强运营商

排斥运营商

Example    Name              Effect
---------------------------------------------------------------------
++$a       Pre-increment     Increments $a by one, then returns $a.
$a++       Post-increment    Returns $a, then increments $a by one.
--$a       Pre-decrement     Decrements $a by one, then returns $a.
$a--       Post-decrement    Returns $a, then decrements $a by one.

它可以在变量之前或之后进行。

例如:

$apples = 10;
for ($i = 0; $i < 10; ++$i) {
    echo 'I have ' . $apples-- . " apples. I just ate one.\n";
}

活例子

在上述情况下 ++$i 使用,因为它更快。

预增略略快一点,因为它真的增加了变量,然后“返回”结果,后增略创造了一个特殊变量,复制了第一个变量的值,只有在第一个变量被使用后,它将其值取代为第二个。

但是,你必须使用 $ 苹果 - 因为首先,你想显示目前的苹果数量,然后你想从它中提取一个。

$i = "a";
while ($i < "c") {
    echo $i++;
}

一旦 z 到达 aa 是下一个,等等。

请注意,字符变量可以增加,但不能减少,即使如此,只有平坦的 ASCII 字符(a-z 和 A-Z)得到支持。


Stack Overflow 文章:

理解增加


逻辑操作员:


比较运营商:


算法操作员:

-$a : 相反的 $a. $a + $b : 合金的 $a 和 $b. $a - $b : 差异的 $a 和 $b. $a * $b : 产品的 $a 和 $b. $a / $b : 比例的 $a 和 $b. $a % $b : 剩余的 $a 分为 $b. $a ** $b : 增加 $a 到 $b 的功率的结果(引入 PHP 5.6)




紧密运营商:


Array 运营商:


任命运营商:


笔记

饰 Curly Braces

区块 - curly braces/no curly braces? Curly braces in string in PHP curly braces in array notation

关于最后一篇文章的一些话

$x[4] = 'd'; // it works
$x{4} = 'd'; // it works

$echo $x[4]; // it works
$echo $x{4}; // it works

$x[] = 'e'; // it works
$x{} = 'e'; // does not work

$x = [1, 2]; // it works
$x = {1, 2}; // does not work

echo "${x[4]}"; // it works
echo "${x{4}}"; // does not work

echo "{$x[4]}"; // it works
echo "{$x{4}}"; // it works
Syntax Name Description
x == y Equality true if x and y have the same key/value pairs
x != y Inequality true if x is not equal to y
x === y Identity true if x and y have the same key/value pairs
in the same order and of the same types
x !== y Non-identity true if x is not identical to y
x <=> y Spaceship 0 if x is equal to y, greater than 0 if x > y, less than 0 if x < y
++x Pre-increment Increments x by one, then returns x
x++ Post-increment Returns x, then increments x by one
--x Pre-decrement Decrements x by one, then returns x
x-- Post-decrement Returns x, then decrements x by one
x and y And true if both x and y are true. If x=6, y=3 then
(x < 10 and y > 1) returns true
x && y And true if both x and y are true. If x=6, y=3 then
(x < 10 && y > 1) returns true
x or y Or true if any of x or y are true. If x=6, y=3 then
(x < 10 or y > 10) returns true
x || y Or true if any of x or y are true. If x=6, y=3 then
(x < 3 || y > 1) returns true
a . b Concatenation Concatenate two strings: "Hi" . "Ha"

__DIR__: 文件的目录. 如果在其中使用,则包含的文件的目录将返回. 这相当于 dirname(__FILE__). 该目录名称没有追踪漏洞,除非它是根目录。

__CLASS__: 类名. (添加到 PHP 4.3.0) 由于 PHP 5 这个常态返回类名,因为它被宣言(案例敏感)。 在 PHP 4 它的值总是低下。 类名包含它被宣言的名称空间(例如 Foo\Bar)。 请注意,因为 PHP 5.4 __CLASS__ 也在特征中工作. 当用于特征方法时, __CLASS__ 是使用特征的类名。

__NAMESPACE__:当前名称空间的名称(案例敏感)。此连续定义为编译时间(在 PHP 5.3.0 中添加)。