我应该在这里做什么?



=& 参考

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

















PHP 中的奇怪打印行为?



= 任命运营商

三种不同的平等


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


=== 比较运营商






比特币运营商


上一篇: 逻辑运营商












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


PHP 短标签是否可用?


二角形字符范围




# One-line shell 风格评论


NullSafe Operator 通话(自 PHP 8.0 以来)

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



当前回答

== 用于检查平等而不考虑变量数据类型

=== 用于检查变量值和数据类型的平等。

例子

A = 5 美元

如果($a = = 5 ) - 将评估为真实的如果($a = 5 ) - 将评估为真实的,因为同时比较这两个值 PHP 内部将该行值转换为整体,然后比较两个值如果($a = 5 ) - 将评估为真实的如果($a = 5 ) - 将评估为虚假的,因为值是 5,但这个值5 不是整体。

其他回答

增加 / 减少运营商

+ 增强运营商

排斥运营商

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 字符串功能時,.. 行動表達一個包含的字符範圍. a-e 等於 abcde。

echo trim('adobe', 'a..e');

印刷:

o

原始 PHP 功能,允许双点范围合成

#[] 自 PHP 8 以来的属性

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

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

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"

在 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"