我应该在这里做什么?



=& 参考

参考任务操作员在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、来源:PHP手册):

Integers, Floats, Strings, Arrays & Objects for 三路比较变量。

// Integers
echo 10 <=> 10; // 0
echo 10 <=> 20; // -1
echo 20 <=> 10; // 1

// Floats
echo 1.5 <=> 1.5; // 0
echo 1.5 <=> 2.5; // -1
echo 2.5 <=> 1.5; // 1

// Strings
echo "a" <=> "a"; // 0
echo "a" <=> "b"; // -1
echo "b" <=> "a"; // 1
// Comparison is case-sensitive
echo "B" <=> "a"; // -1

echo "a" <=> "aa"; // -1
echo "zz" <=> "aa"; // 1

// Arrays
echo [] <=> []; // 0
echo [1, 2, 3] <=> [1, 2, 3]; // 0
echo [1, 2, 3] <=> []; // 1
echo [1, 2, 3] <=> [1, 2, 1]; // 1
echo [1, 2, 3] <=> [1, 2, 4]; // -1

// Objects
$a = (object) ["a" => "b"]; 
$b = (object) ["a" => "b"]; 
echo $a <=> $b; // 0

$a = (object) ["a" => "b"]; 
$b = (object) ["a" => "c"]; 
echo $a <=> $b; // -1

$a = (object) ["a" => "c"]; 
$b = (object) ["a" => "b"]; 
echo $a <=> $b; // 1

// only values are compared
$a = (object) ["a" => "b"]; 
$b = (object) ["b" => "b"]; 
echo $a <=> $b; // 1

其他回答

三 DOTS 作为 Splat Operator (...) (自 PHP 5.6 以来)

PHP 有一个操作员“......(三点)被称为Splat操作员,它被用来通过一个函数中的参数的任意数量,这种函数被称为变量函数。

例子1:

<?php
function calculateNumbers(...$params){
    $total = 0;
    foreach($params as $v){
        $total = $total + $v;
    }
    return $total;
}
 
echo calculateNumbers(10, 20, 30, 40, 50);
 
//Output 150
?>

计算的每个论点 数字() 函数通过 $params 作为一个序列,当使用 "... ".

有很多不同的方式使用“......”运营商,以下是几个例子:

例子2:

<?php
function calculateNumbers($no1, $no2, $no3, $no4, $no5){
    $total = $no1 + $no2 + $no3 + $no4 + $no5;
    return $total;
}
 
$numbers = array(10, 20, 30, 40, 50);
echo calculateNumbers(...$numbers);
 
//Output 150
?>

例子3:

<?php
function calculateNumbers(...$params){
    $total = 0;
    foreach($params as $v){
        $total = $total + $v;
    }
    return $total;
}
$no1 = 70;
$numbers = array(10, 20, 30, 40, 50);
echo calculateNumbers($no1, ...$numbers);
 
//Output 220
?>

例子4:

<?php
function calculateNumbers(...$params){
    $total = 0;
    foreach($params as $v){
        $total = $total + $v;
    }
    return $total;
}
 
$numbers1 = array(10, 20, 30, 40, 50);
$numbers2 = array(100, 200, 300, 400, 500);
echo calculateNumbers(...$numbers1, ...$numbers2);
 
//Output 1650
 
?>

值得注意的是,变量参数不能用所谓的论点瞄准。

例子5:

<?php
function sumIntegers(int ...$params): int {
    $sum = 0;
    foreach($params as $value){
        $sum += $value;
    }
    return $sum;
}


echo sumIntegers(params: [1, 2, 3, 4]);     
// $params will be equal to ['params' => [1, 2, 3, 4]] in sumIntegers
// throws TypeError sumIntegers(): Argument #1 must be of type int, array given 

echo sumIntegers(arbitrary_name: 1, another_name: 2);
// $params will be equal to ['arbitrary_name' => 1, 'another_name' => 2] in sumIntegers
// Outputs: 3 
?>

使用未包装的连接序列作为函数呼叫的参数具有相同的效果,如使用每个关键值对作为一个名为论点的函数呼叫。

例子6:

<?php
function fullName(string $first_name, ?string $middle_name = null, ?string $last_name = null): string {
    return trim("$first_name|$middle_name|$last_name");
}

$params = ['first_name' => 'John', 'last_name' => 'Doe'];
echo fullName(...$params);
// same as fullName(first_name: 'John', last_name: 'Doe')

// outputs 'John||Doe'
?>

它可以用来将命名的论点转移到某种东西,如一个无缝函数呼叫或一个班级。

例子7:

<?php
function throw_exception(string $exception, ...$parameters) {
    throw new $exception(...$parameters);
}

throw_exception(exception: 'Exception', code: 123);
// executes throw new Exception(...['code' => 123])
// which is the same as throw new Exception(code: 123);
?>

增加 / 减少运营商

+ 增强运营商

排斥运营商

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 文章:

理解增加

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"

Null Coalesce 操作器 “??” (在 PHP 7 中添加)

不是一个运营商最受欢迎的名称,但PHP 7带来了相当方便的零冷却,所以我认为我会分享一个例子。

在PHP 5中,我们已经有一个特纳操作器,测试一个值,然后返回第二个元素,如果它返回真实,第三个元素,如果它没有:

echo $count ? $count : 10; // outputs 10

在PHP 7中,我们还获得了操作员,而不是指示极端的混乱,这就是我通常会使用两个问题标志相结合的方式,而不是允许我们连接一行值。

// $a is not set
$b = 16;

echo $a ?? 2; // outputs 2
echo $a ?? $b ?? 7; // outputs 16

这个结构是有用的,以优先考虑一个或多个值来自用户输入或现有配置,并安全地落后于一个特定的默认设置,如果该配置缺乏。

比特币运营商

什么是比特? 比特是 1 或 0 的代表性 基本上是 OFF(0) 和 ON(1)

什么是比特?一个比特是由8位组成的,一个比特的最高值是255,这意味着每个比特是设置的。

-------------------------------------------
|      1 Byte ( 8 bits )                  |
-------------------------------------------
|Place Value | 128| 64| 32| 16| 8| 4| 2| 1|     
-------------------------------------------

此分類上一篇: 1 Byte

1 + 2 + 4 + 8 + 16 + 32 + 64 + 128 = 255 (1 位元)

一些好理解的例子

“和”运营商: &

$a =  9;
$b = 10;
echo $a & $b;

这将产生第8号为什么? 好吧,让我们看看使用我们的表例子。

-------------------------------------------
|      1 Byte ( 8 bits )                  |
-------------------------------------------
|Place Value | 128| 64| 32| 16| 8| 4| 2| 1|     
-------------------------------------------
|      $a    |   0|  0|  0|  0| 1| 0| 0| 1|    
-------------------------------------------
|      $b    |   0|  0|  0|  0| 1| 0| 1| 0|
------------------------------------------- 
|      &     |   0|  0|  0|  0| 1| 0| 0| 0|
------------------------------------------- 

所以你可以从桌子上看到他们共享的唯一片段是8位。

第二个例子

$a =  36;
$b = 103;
echo $a & $b; // This would output the number 36.
$a = 00100100
$b = 01100111

兩個共享比特是32和4,當添加一起返回36。

“黄金”运营商: <unk>

$a =  9;
$b = 10;
echo $a | $b;

第11章 为什么?

-------------------------------------------
|      1 Byte ( 8 bits )                  |
-------------------------------------------
|Place Value | 128| 64| 32| 16| 8| 4| 2| 1|     
-------------------------------------------
|      $a    |   0|  0|  0|  0| 1| 0| 0| 1|    
-------------------------------------------
|      $b    |   0|  0|  0|  0| 1| 0| 1| 0|
------------------------------------------- 
|      |     |   0|  0|  0|  0| 1| 0| 1| 1|
-------------------------------------------

您将注意到,我们有 3 个字符串设置,在 8、2 和 1 个列中。