我应该在这里做什么?



=& 参考

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

















PHP 中的奇怪打印行为?



= 任命运营商

三种不同的平等


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


=== 比较运营商






比特币运营商


上一篇: 逻辑运营商












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


PHP 短标签是否可用?


二角形字符范围




# One-line shell 风格评论


NullSafe Operator 通话(自 PHP 8.0 以来)

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



当前回答

零退款类型申报

PHP 7 添加支持返回类型声明. 类似于论点类型声明,返回类型声明指定将从函数返回的值类型. 相同的类型可用于返回类型声明,如论点类型声明。

严格打字也会影响返回类型声明. 在默认的弱模式中,返回值将被强迫到正确的类型,如果它们已经不属于该类型. 在强劲模式中,返回值必须属于正确的类型,否则,将被扔到 TypeError。

对于 PHP 7.1.0 而言,返回值可以通过预定类型名称与问答标(?)来标记为无值,这意味着函数返回指定类型或 NULL。

<?php
function get_item(): ?string {
    if (isset($_GET['item'])) {
        return $_GET['item'];
    } else {
        return null;
    }
}
?>

来源

其他回答

<=> 航天运营商

在 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
});

运营商类型

instanceof 是用来确定一个 PHP 变量是否是一个特定的类别的即时对象。

<?php
class mclass { }
class sclass { }
$a = new mclass;
var_dump($a instanceof mclass);
var_dump($a instanceof sclass);

上面的例子将产生:

bool(true)
bool(false)

原因: 上例 $a 是 mclass 的对象,所以只使用 mclass 数据而不是 sclass 的例子。

继承的例子

<?php 
class pclass { } 
class childclass extends pclass { } 
$a = new childclass; 
var_dump($a instanceof childclass); 
var_dump($a instanceof pclass);

上面的例子将产生:

bool(true)
bool(true)

与克隆的例子

<?php 
class cloneable { } 
$a = new cloneable;
$b = clone $a; 
var_dump($a instanceof cloneable); 
var_dump($b instanceof cloneable);

上面的例子将产生:

bool(true)
bool(true)
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"

_ Alias for gettext( )

作为 _() 的字符“_”是 gettext() 函数的标志。

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
?>