我应该在这里做什么?



=& 参考

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

















PHP 中的奇怪打印行为?



= 任命运营商

三种不同的平等


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


=== 比较运营商






比特币运营商


上一篇: 逻辑运营商












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


PHP 短标签是否可用?


二角形字符范围




# One-line shell 风格评论


NullSafe Operator 通话(自 PHP 8.0 以来)

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



当前回答

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

其他回答

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

问答:

什么是“”意思?


答案:

编制:

要了解这一点,我们必须知道什么是协同序列,当一个常规程序员认为一个序列时出现的第一件事(在PHP中)会是类似于:

$myArray1 = array(2016, "hello", 33);//option 1

$myArray2 = [2016, "hello", 33];//option 2

$myArray3 = [];//option 3
$myArray3[] = 2016; 
$myArray3[] = "hello"; 
$myArray3[] = 33;

在哪里,如果我们想在代码的某些后续部分召唤序列,我们可以:

echo $myArray1[1];// output: hello
echo $myArray2[1];// output: hello
echo $myArray3[1];// output: hello

差异(序列和附属序列之间):

在附属序列的声明中,您不只是将您想要插入序列的值,但您还将所需的指数值(称为密钥)插入,当您在代码的后部分召唤序列时使用。

例如:

$myArray1 = array( 
    "Year" => 2016, 
    "Greetings" => "hello", 
    "Integer_value" => 33);//option 1

$myArray2 = [ 
    "Year" =>  2016, 
    "Greetings" => "hello", 
    "Integer_value" => 33];//option 2

$myArray3 = [];//option 3
$myArray3["Year"] = 2016; 
$myArray3["Greetings"] = "hello"; 
$myArray3["Integer_value"] = 33;

现在,以获得与以前相同的产量,关键值将用于雷索指数:

echo $myArray1["Greetings"];// output: hello
echo $myArray2["Greetings"];// output: hello
echo $myArray3["Greetings"];// output: hello

在 PHP 8 中

而不是写经典!== null 你可以使用操作员写一行代码,代码变得相当清晰:

之前:

$firsName =  null;
if ($session !== null) {
    $user = $session->user;
    if ($user !== null) {
        $name = $user->getName();
        if ($name !== null) {
            $firstName = $name->firstName;
        }
    }
}

之后:

$firsName = $session?->user?->getName()?->firstName;

使用对比而不是交换. 对比表达使用严格的比较(===) 相反. 对比是严格的,无论严格的类型。

之前:

switch ('A') {
    case 'A':
        echo "found A";
        break;
    case 'B':
        echo "found B";
        break;
}
// Result: "found A"

之后:

echo match ('A') {
  'A' => "found A",
  'B' => "found B",
};
// Result: "found A"
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"

饰 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