我应该在这里做什么?



=& 参考

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

















PHP 中的奇怪打印行为?



= 任命运营商

三种不同的平等


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


=== 比较运营商






比特币运营商


上一篇: 逻辑运营商












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


PHP 短标签是否可用?


二角形字符范围




# One-line shell 风格评论


NullSafe Operator 通话(自 PHP 8.0 以来)

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



当前回答

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"

其他回答

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"

增加 / 减少运营商

+ 增强运营商

排斥运营商

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

理解增加

#[] 自 PHP 8 以来的属性

您可以写 #[attribute_name] 从 PHP 8. 这是一个属性在 PHP (也 Rust 和 C#). 其他语言可能使用名称如笔记(Java)或装饰(Python,JavaScript)为类似的功能. 在 PHP 8 之前, # [任何东西] 将是一个评论,直到线的结束(因为 # 开始一个评论在 PHP 。 所以,如果一个属性是线上的最后一件事,它将被忽略在句子中

RFC Syntax RFC(原始 RFC 已更改) 文档

问答:

什么是“”意思?


答案:

编制:

要了解这一点,我们必须知道什么是协同序列,当一个常规程序员认为一个序列时出现的第一件事(在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

NullSafe 操作员 “?>” 从 php8

在PHP8已经接受了这个新的运营商,你可以在这里找到文档。>它是NullSafe运营商,它返回零,如果你试图引用功能或从零获得值。

例子:

<?php
$obj = null;
$obj = $obj?->attr; //return null
$obj = ?->funct(); // return null
$obj = $objDrive->attr; // Error: Trying to get property 'attr' of non-object
?>