我应该在这里做什么?



=& 参考

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

















PHP 中的奇怪打印行为?



= 任命运营商

三种不同的平等


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


=== 比较运营商






比特币运营商


上一篇: 逻辑运营商












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


PHP 短标签是否可用?


二角形字符范围




# One-line shell 风格评论


NullSafe Operator 通话(自 PHP 8.0 以来)

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



当前回答

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

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

其他回答

问题:

“&”在PHP中意味着什么?

PHP “&” 操作员

使生活更容易,一旦我们习惯了(仔细检查下面的例子)

通常会设置为 $a 和 $b 的检查比特。

你甚至注意到这些电话是如何工作的吗?

   error_reporting(E_ERROR | E_WARNING | E_PARSE);
    error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE);
    error_reporting(E_ALL & ~E_NOTICE);
    error_reporting(E_ALL);

因此,上面的一切背后是比特币运营商和比特币的游戏。

其中一个有用的案例是简单的配置,如下所示,这样一个完整的字段可以为您存储成千上万的组合。

大多数人已经阅读了这些文件,但并没有依赖于这些小型运营商的现实世界使用案例。

假设你会爱的

<?php

class Config {

    // our constants must be 1,2,4,8,16,32,64 ....so on
    const TYPE_CAT=1;
    const TYPE_DOG=2;
    const TYPE_LION=4;
    const TYPE_RAT=8;
    const TYPE_BIRD=16;
    const TYPE_ALL=31;

    private $config;

    public function __construct($config){
        $this->config=$config;

        if($this->is(Config::TYPE_CAT)){
            echo 'cat ';
        }
        if($this->is(Config::TYPE_DOG)){
            echo 'dog ';
        }
        if($this->is(Config::TYPE_RAT)){
            echo 'rat ';
        }
        if($this->is(Config::TYPE_LION)){
            echo 'lion ';
        }
        if($this->is(Config::TYPE_BIRD)){
            echo 'bird ';
        }
        echo "\n";
    }

    private function is($value){
        return $this->config & $value;
    }
}

new Config(Config::TYPE_ALL);
// cat dog rat lion bird
new Config(Config::TYPE_BIRD);
//bird
new Config(Config::TYPE_BIRD | Config::TYPE_DOG);
//dog bird
new Config(Config::TYPE_ALL & ~Config::TYPE_DOG & ~Config::TYPE_CAT);
//rat lion bird

航天运营商 <=> (添加到 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

NullSafe 运营商

在 PHP 8.0 中添加

它是 NullSafe 运营商,如果您尝试引用函数或从零中获取值,则返回零。

$objDrive = null;
$drive = $objDrive?->func?->getDriver()?->value; //return null
$drive = $objDrive->func->getDriver()->value; // Error: Trying to get property 'func' of non-object

Nullsafe 运营商不使用序列密钥:

$drive['admin']?->getDriver()?->value //Warning: Trying to access array offset on value of type null

$drive = [];
$drive['admin']?->getAddress()?->value //Warning: Undefined array key "admin"

零加热运营商(??)

此操作员已在 PHP 7.0 中添加到常见情况下,需要与 isset() 一起使用终端操作员,如果存在并非 NULL,则将其第一个操作员返回;否则将其第二个操作员返回。

<?php
// Fetches the value of $_GET['user'] and returns 'nobody'
// if it does not exist.
$username = $_GET['user'] ?? 'nobody';
// This is equivalent to:
$username = isset($_GET['user']) ? $_GET['user'] : 'nobody';

// Coalescing can be chained: this will return the first
// defined value out of $_GET['user'], $_POST['user'], and
// 'nobody'.
$username = $_GET['user'] ?? $_POST['user'] ?? 'nobody';
?>

三 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);
?>