参考——这个符号在 PHP 中是什么意思?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3737139/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Reference — What does this symbol mean in PHP?
提问by Gordon
What is this?
这是什么?
This is a collection of questions that come up every now and then about syntax in PHP. This is also a Community Wiki, so everyone is invited to participate in maintaining this list.
这是一个不时出现的关于 PHP 语法的问题集合。这也是一个社区维基,所以邀请大家参与维护这个列表。
Why is this?
为什么是这样?
It used to be hard to find questions about operators and other syntax tokens.1
The main idea is to have links to existing questions on Stack Overflow, so it's easier for us to reference them, not to copy over content from the PHP Manual.
过去很难找到有关运算符和其他语法标记
的问题。1主要思想是在 Stack Overflow 上提供指向现有问题的链接,因此我们更容易参考它们,而不是复制 PHP 手册中的内容。
Note: Since January 2013, Stack Overflow does support special characters. Just surround the search terms by quotes, e.g. [php] "==" vs "==="
注意:自 2013 年 1 月起,Stack Overflow确实支持特殊字符。只需用引号将搜索词括起来,例如[php] "==" vs "==="
What should I do here?
我应该在这里做什么?
If you have been pointed here by someone because you have asked such a question, please find the particular syntax below. The linked pages to the PHP manualalong with the linked questions will likely answer your question then. If so, you are encouraged to upvote the answer. This list is not meant as a substitute to the help others provided.
如果您因为问过这样的问题而被某人指出到这里,请在下面找到特定的语法。PHP 手册的链接页面以及链接的问题很可能会回答您的问题。如果是这样,我们鼓励您对答案投赞成票。此列表不能替代其他人提供的帮助。
The List
列表
If your particular token is not listed below, you might find it in the List of Parser Tokens.
如果您的特定令牌未在下面列出,您可能会在解析器令牌列表中找到它。
&
Bitwise Operatorsor References
- What does it mean to start a PHP function with an ampersand?
- Understanding PHP & (ampersand, bitwise and) operator
- PHP "&" operator
- Difference between & and && in PHP
- What does "&" mean here in PHP?
- What does "&" mean in this case?
- What does the "&" sign mean in PHP?
- What does this signature mean (&) in PHP?
- How does the "&" operator work in a PHP function?
- What does & in &2 mean in PHP?
- When should I use a bitwise operator?
- Is there ever a need to use ampersand in front of an object?(&$)
- PHP 函数用 & 符号开头是什么意思?
- 理解 PHP & (&, bitwise and) 运算符
- PHP“&”运算符
- PHP中&和&&的区别
- PHP中的“&”是什么意思?
- 在这种情况下,“&”是什么意思?
- PHP中的“&”符号是什么意思?
- 这个签名在 PHP 中是什么意思 (&)?
- "&" 运算符如何在 PHP 函数中工作?
- & in &2 在 PHP 中是什么意思?
- 什么时候应该使用按位运算符?
- 是否需要在对象前使用&符号?(&$)
=&
参考
- Reference assignment operator in PHP, =&
- What do the "=&" and "&=" operators in PHP mean?
- What do the '&=' and '=&' operators do?
- What does =& mean in PHP?
&=
按位运算符
&&
逻辑运算符
- 'AND' vs '&&' as operator
- Difference between & and && in PHP
- Is there any difference between "and" and "&&" operators in PHP?
- PHP - and / or keywords
- What does the percent sign mean in PHP?
- What is the PHP operator % and how do I use it in real-world examples?
!!
逻辑运算符
- What is the use of the @ symbol in PHP?
- 'At' symbol before variable name in PHP: @$_POST
- PHP functions and @functions
- Should I use @ in my PHP code?
- What does @ mean in PHP?
?:
三元运算符
- What are the PHP operators "?" and ":" called and what do they do?
- ?: operator (the 'Elvis operator') in PHP
- Where can I read about conditionals done with "?" and ":" (colon)?
- Using PHP 5.3 ?: operator
- 什么是 PHP 运算符“?” 和 ":" 调用它们做什么?
- ?: PHP 中的运算符(“猫王运算符”)
- 我在哪里可以阅读有关使用“?”完成的条件的信息 和“:”(冒号)?
- 使用 PHP 5.3 ?: 运算符
??
Null Coalesce Operator(since PHP 7)
??
空合并运算符(自 PHP 7 起)
?string
?int
?array
?bool
?float
Nullable return type declaration(since PHP 7.1)
?string
?int
?array
?bool
?float
可空返回类型声明(自 PHP 7.1 起)
:
Alternative syntax for control structures, Ternary Operator
::
范围解析运算符
- What do two colons mean in PHP?
- What's the meaning of the PHP token name T_PAAMAYIM_NEKUDOTAYIM?
- What's the difference between :: (double colon) and -> (arrow) in PHP?
- What exactly are late static bindings in PHP?
- static::staticFunctionName()
- Unexpected T_PAAMAYIM_NEKUDOTAYIM, expecting T_NS_Separator
- PHP中的两个冒号是什么意思?
- PHP 令牌名称 T_PAAMAYIM_NEKUDOTAYIM 的含义是什么?
- PHP 中的 ::(双冒号)和 ->(箭头)有什么区别?
- PHP 中的后期静态绑定到底是什么?
- 静态::静态函数名()
- 意外的 T_PAAMAYIM_NEKUDOTAYIM,期待 T_NS_Separator
\
命名空间
->
类和对象
- What is the "->" PHP operator called and how do you say it when reading code out loud?
- Where do we use the object operator "->" in PHP?
- What's the difference between :: (double colon) and -> (arrow) in PHP?
- What does the PHP syntax $var1->$var2 mean?
- What does "->" mean/refer to in PHP?
- PHP 运算符“->”是什么?大声朗读代码时怎么说?
- 我们在哪里使用 PHP 中的对象运算符“->”?
- PHP 中的 ::(双冒号)和 ->(箭头)有什么区别?
- PHP 语法 $var1->$var2 是什么意思?
- “->”在PHP中是什么意思/指代?
=>
Arrays
=>
数组
>>
按位运算符
<<
按位运算符
- What does <<<END mean in PHP?
- PHP expression <<<EOB
- In PHP, what does "<<<" represent?
- Using <<<CON in PHP
- What's this kind of syntax in PHP?
==
比较运算符
- How do the PHP equality (== double equals) and identity (=== triple equals) comparison operators differ?
- PHP != and == operators
- The 3 different equals
- Type-juggling and (strict) greater/lesser-than comparisons in PHP
===
比较运算符
- What does "===" mean?
- How do the PHP equality (== double equals) and identity (=== triple equals) comparison operators differ?
- The 3 different equals
- Type-juggling and (strict) greater/lesser-than comparisons in PHP
!==
比较运算符
!=
比较运算符
- PHP != and == operators
- Is there a difference between !== and != in PHP?
- comparing, !== versus !=
- What is the difference between <> and !=
<>
比较运算符
- PHP operator <>
- PHP's <> operator
- What is the difference between <> and !=
- Type-juggling and (strict) greater/lesser-than comparisons in PHP
<=>
Comparison Operators(since PHP 7.0)
<=>
比较运算符(自 PHP 7.0 起)
- What is the difference between the | and || operators?
- What Does Using A Single Pipe '|' In A Function Argument Do?
||
逻辑运算符
- What is the difference between the | and || operators?
- PHP - and / or keywords
- What exactly does || mean?
- The behaviour of the or operator in PHP
+
Arithmetic Operators, Array Operators
+=
and -=
Assignment Operators
+=
和-=
赋值运算符
++
and --
Incrementing/Decrementing Operators
++
和--
递增/递减运算符
.=
赋值运算符
- Difference between period and comma when concatenating with echo versus return?
- What does a . (dot) do in PHP?
,
函数参数
,
变量声明
$$
变量变量
- What does $$ (dollar dollar or double dollar) mean in PHP?
- what is "$$" in PHP
- $function() and $$variable
<?=
短开标签
[]
Arrays(short syntax since PHP 5.4)
[]
数组(自 PHP 5.4 起的简短语法)
- PHP arrays... What is/are the meaning(s) of an empty bracket?
- What is the meaning of []
- Php array_push() vs myArray[]
- What does [] mean when reading from a PHP array?
- Shorthand for arrays: literal
$var = []
empty array
<?
开始和结束标签
...
Argument unpacking(since PHP 5.6)
...
参数解包(自 PHP 5.6 起)
**
Exponentiation(since PHP 5.6)
**
求幂(自 PHP 5.6 起)
:?
可空返回类型
采纳答案by Peter Ajtai
Incrementing / Decrementing Operators
++
increment operator
++
增量运算符
--
decrement operator
--
递减运算符
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.
These can go before or after the variable.
这些可以在变量之前或之后。
If put before the variable, the increment/decrement operation is done to the variable firstthen the result is returned. If put after the variable, the variable is firstreturned, then the increment/decrement operation is done.
如果变量之前说,该递增/递减操作完成的变量第一,然后返回结果。如果放在变量之后,则先返回变量,然后进行递增/递减操作。
For example:
例如:
$apples = 10;
for ($i = 0; $i < 10; ++$i) {
echo 'I have ' . $apples-- . " apples. I just ate one.\n";
}
In the case above ++$i
is used, since it is faster. $i++
would have the same results.
在上述情况下++$i
使用,因为它更快。$i++
会有相同的结果。
Pre-increment is a little bit faster because it really increments the variable and after that 'returns' the result. Post-increment creates a special variable, copies there the value of the first variable and only after the first variable is used, replaces its value with second's.
预增量有点快,因为它确实增加了变量,然后“返回”了结果。后增量创建一个特殊变量,在那里复制第一个变量的值,只有在使用第一个变量后,才用第二个变量替换它的值。
However, you must use $apples--
, since first, you want to display the current number of apples, and thenyou want to subtract one from it.
但是,您必须使用$apples--
,因为首先要显示当前苹果的数量,然后要从中减去一个。
You can also increment letters in PHP:
您还可以在 PHP 中增加字母:
$i = "a";
while ($i < "c") {
echo $i++;
}
Once z
is reached aa
is next, and so on.
一旦z
到达aa
是下一个,依此类推。
Note that character variables can be incremented but not decremented and even so only plain ASCII characters (a-z and A-Z) are supported.
请注意,字符变量可以递增但不能递减,即使如此,也仅支持纯 ASCII 字符(az 和 AZ)。
Stack Overflow Posts:
堆栈溢出帖子:
回答by Ankur Saxena
Bitwise Operator
按位运算符
What is a bit? A bit is a representation of 1 or 0. Basically OFF(0) and ON(1)
什么是一点?位是 1 或 0 的表示。基本上是 OFF(0) 和 ON(1)
What is a byte? A byte is made up of 8 bits and the highest value of a byte is 255, which would mean every bit is set. We will look at why a byte's maximum value is 255.
什么是字节?一个字节由 8 位组成,一个字节的最高值为 255,这意味着每一位都被设置。我们将看看为什么一个字节的最大值是 255。
-------------------------------------------
| 1 Byte ( 8 bits ) |
-------------------------------------------
|Place Value | 128| 64| 32| 16| 8| 4| 2| 1|
-------------------------------------------
This representation of 1 Byte
1 Byte 的这种表示
1 + 2 + 4 + 8 + 16 + 32 + 64 + 128 = 255 (1 Byte)
1 + 2 + 4 + 8 + 16 + 32 + 64 + 128 = 255(1 个字节)
A few examples for better understanding
一些示例以更好地理解
The "AND" operator: &
“与”运算符: &
$a = 9;
$b = 10;
echo $a & $b;
This would output the number 8. Why? Well let's see using our table example.
这将输出数字 8。为什么?好吧,让我们看看使用我们的表格示例。
-------------------------------------------
| 1 Byte ( 8 bits ) |
-------------------------------------------
|Place Value | 128| 64| 32| 16| 8| 4| 2| 1|
-------------------------------------------
| $a | 0| 0| 0| 0| 1| 0| 0| 1|
-------------------------------------------
| $b | 0| 0| 0| 0| 1| 0| 1| 0|
-------------------------------------------
| & | 0| 0| 0| 0| 1| 0| 0| 0|
-------------------------------------------
So you can see from the table the only bit they share together is the 8 bit.
因此,您可以从表中看到它们共享的唯一位是 8 位。
Second example
第二个例子
$a = 36;
$b = 103;
echo $a & $b; // This would output the number 36.
$a = 00100100
$b = 01100111
The two shared bits are 32 and 4, which when added together return 36.
两个共享位是 32 和 4,它们相加后返回 36。
The "Or" operator: |
“或”运算符: |
$a = 9;
$b = 10;
echo $a | $b;
This would output the number 11. Why?
这将输出数字 11。为什么?
-------------------------------------------
| 1 Byte ( 8 bits ) |
-------------------------------------------
|Place Value | 128| 64| 32| 16| 8| 4| 2| 1|
-------------------------------------------
| $a | 0| 0| 0| 0| 1| 0| 0| 1|
-------------------------------------------
| $b | 0| 0| 0| 0| 1| 0| 1| 0|
-------------------------------------------
| | | 0| 0| 0| 0| 1| 0| 1| 1|
-------------------------------------------
You will notice that we have 3 bits set, in the 8, 2, and 1 columns. Add those up: 8+2+1=11.
您会注意到我们在 8、2 和 1 列中设置了 3 位。把它们加起来:8+2+1=11。
回答by Sherif
<=>
Spaceship Operator
<=>
飞船操作员
Added in PHP 7
在 PHP 7 中添加
The spaceship operator<=>
is the latest comparison operator added in PHP 7. It is a non-associativebinary operator with the same precedence as equality operators (==
, !=
, ===
, !==
). This operator allows for simpler three-way comparison between left-hand and right-hand operands.
该飞船操作<=>
是在PHP 7增加了最新的运营商相比,它是一种非关联二元运算符具有相同优先级的平等运算符(==
,!=
,===
,!==
)。此运算符允许在左手和右手操作数之间进行更简单的三向比较。
The operator results in an integer expression of:
该运算符产生一个整数表达式:
0
when both operands are equal- Less than
0
when the left-hand operand is less than the right-hand operand - Greater than
0
when the left-hand operand is greater than the right-hand operand
0
当两个操作数相等时- 小于
0
当左边的操作数小于右边的操作数 - 大于
0
当左侧操作数大于右侧操作数时
e.g.
例如
1 <=> 1; // 0
1 <=> 2; // -1
2 <=> 1; // 1
A good practical application of using this operator would be in comparison type callbacks that are expected to return a zero, negative, or positive integer based on a three-way comparison between two values. The comparison function passed to usort
is one such example.
使用此运算符的一个很好的实际应用是比较类型的回调,这些回调预计会基于两个值之间的三路比较返回零、负或正整数。传递给的比较函数usort
就是这样一个例子。
Before PHP 7 you would write...
在 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;
}
});
Since PHP 7 you can write...
从 PHP 7 开始,您可以编写...
$arr = [4,2,1,3];
usort($arr, function ($a, $b) {
return $a <=> $b;
});
回答by Habeeb Perwad
回答by Ankur Saxena
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 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 x=6 y=3
(x < 10 and y > 1) returns true
x && y And True if both x and y are true x=6 y=3
(x < 10 && y > 1) returns true
x or y Or True if any of x or y are true x=6 y=3
(x < 10 or y > 10) returns true
x || y Or True if any of x or y are true x=6 y=3
(x < 3 || y > 1) returns true
a . b Concatenation Concatenate two strings: "Hi" . "Ha"
回答by Sumoanand
Magic constants: Although these are not just symbols but important part of this token family. There are eight magical constants that change depending on where they are used.
魔法常数:虽然这些不仅仅是符号,而是这个令牌家族的重要组成部分。有八个神奇的常数会根据它们的使用位置而变化。
__LINE__
: The current line number of the file.
__LINE__
: 文件的当前行号。
__FILE__
: The full path and filename of the file. If used inside an include, the name of the included file is returned. Since PHP 4.0.2, __FILE__
always contains an absolute path with symlinks resolved whereas in older versions it contained relative path under some circumstances.
__FILE__
: 文件的完整路径和文件名。如果在包含中使用,则返回包含文件的名称。自 PHP 4.0.2 起,__FILE__
始终包含已解析符号链接的绝对路径,而在旧版本中,它在某些情况下包含相对路径。
__DIR__
: The directory of the file. If used inside an include, the directory of the included file is returned. This is equivalent to dirname(__FILE__)
. This directory name does not have a trailing slash unless it is the root directory. (Added in PHP 5.3.0.)
__DIR__
: 文件的目录。如果在包含中使用,则返回包含文件的目录。这相当于dirname(__FILE__)
. 除非它是根目录,否则此目录名称没有尾部斜杠。(在 PHP 5.3.0 中添加。)
__FUNCTION__
: The function name. (Added in PHP 4.3.0) As of PHP 5 this constant returns the function name as it was declared (case-sensitive). In PHP 4 its value is always lowercased.
__FUNCTION__
: 函数名。(在 PHP 4.3.0 中添加)自 PHP 5 起,此常量返回声明时的函数名称(区分大小写)。在 PHP 4 中,它的值总是小写的。
__CLASS__
: The class name. (Added in PHP 4.3.0) As of PHP 5 this constant returns the class name as it was declared (case-sensitive). In PHP 4 its value is always lowercased. The class name includes the namespace it was declared in (e.g. Foo\Bar
). Note that as of PHP 5.4 __CLASS__
works also in traits. When used in a trait method, __CLASS__
is the name of the class the trait is used in.
__CLASS__
: 类名。(在 PHP 4.3.0 中添加)从 PHP 5 开始,该常量返回声明时的类名(区分大小写)。在 PHP 4 中,它的值总是小写的。类名包括它在其中声明的命名空间(例如Foo\Bar
)。请注意,从 PHP 5.4 开始,__CLASS__
也适用于特征。在 trait 方法__CLASS__
中使用时,是使用该 trait 的类的名称。
__TRAIT__
: The trait name. (Added in PHP 5.4.0) As of PHP 5.4 this constant returns the trait as it was declared (case-sensitive). The trait name includes the namespace it was declared in (e.g. Foo\Bar
).
__TRAIT__
: 特性名称。(在 PHP 5.4.0 中添加)自 PHP 5.4 起,此常量返回声明时的特征(区分大小写)。特征名称包括它在(例如Foo\Bar
)中声明的命名空间。
__METHOD__
: The class method name. (Added in PHP 5.0.0) The method name is returned as it was declared (case-sensitive).
__METHOD__
: 类方法名称。(在 PHP 5.0.0 中添加) 方法名称在声明时返回(区分大小写)。
__NAMESPACE__
: The name of the current namespace (case-sensitive). This constant is defined in compile-time (Added in PHP 5.3.0).
__NAMESPACE__
: 当前命名空间的名称(区分大小写)。该常量在编译时定义(PHP 5.3.0 中添加)。
回答by Maulik patel
Type Operators
类型运算符
instanceof
is used to determine whether a PHP variable is an instantiated object of a certain class.
instanceof
用于判断一个PHP变量是否是某个类的实例化对象。
<?php
class mclass { }
class sclass { }
$a = new mclass;
var_dump($a instanceof mclass);
var_dump($a instanceof sclass);
The above example will output:
上面的例子将输出:
bool(true)
bool(false)
Reason:Above Example $a
is a object of the mclass
so use only a mclass
data not instance of with the sclass
原因:上面的例子$a
是一个对象,mclass
所以只使用mclass
数据而不是实例sclass
Example with inheritance
继承的例子
<?php
class pclass { }
class childclass extends pclass { }
$a = new childclass;
var_dump($a instanceof childclass);
var_dump($a instanceof pclass);
The above example will output:
上面的例子将输出:
bool(true)
bool(true)
Example with Clone
克隆示例
<?php
class cloneable { }
$a = new cloneable;
$b = clone $a;
var_dump($a instanceof cloneable);
var_dump($b instanceof cloneable);
The above example will output:
上面的例子将输出:
bool(true)
bool(true)
回答by John Slegers
An overview of operators in PHP:
PHP 中的运算符概述:
Logical Operators:
逻辑运算符:
- $a && $b :TRUE if both $a and $b are TRUE.
- $a || $b :TRUE if either $a or $b is TRUE.
- $a xor $b :TRUE if either $a or $b is TRUE, but not both.
- ! $a :TRUE if $a is not TRUE.
- $a and $b :TRUE if both $a and $b are TRUE.
- $a or $b :TRUE if either $a or $b is TRUE.
- $a && $b :如果 $a 和 $b 都为 TRUE,则为 TRUE。
- $a || $b :如果 $a 或 $b 为 TRUE,则为 TRUE。
- $a xor $b :如果 $a 或 $b 为 TRUE,则为 TRUE,但不是两者都为 TRUE。
- !$a :如果 $a 不为 TRUE,则为 TRUE。
- $a 和 $b :如果 $a 和 $b 都为 TRUE,则为 TRUE。
- $a 或 $b :如果 $a 或 $b 为 TRUE,则为 TRUE。
Comparison operators:
比较运算符:
- $a == $b :TRUE if $a is equal to $b after type juggling.
- $a === $b :TRUE if $a is equal to $b, and they are of the same type.
- $a != $b :TRUE if $a is not equal to $b after type juggling.
- $a <> $b :TRUE if $a is not equal to $b after type juggling.
- $a !== $b :TRUE if $a is not equal to $b, or they are not of the same type.
- $a < $b: TRUE if $a is strictly less than $b.
- $a > $b: TRUE if $a is strictly greater than $b.
- $a <= $b: TRUE if $a is less than or equal to $b.
- $a >= $b: TRUE if $a is greater than or equal to $b.
- $a <=> $b: An integer less than, equal to, or greater than zero when $a is respectively less than, equal to, or greater than $b. Available as of PHP 7.
- $a ? $b : $c: if $a return $b else return $c (ternary operator)
- $a ?? $c: Same as $a ? $a : $c (null coalescing operator- requires PHP>=7)
- $a == $b :如果 $a 在类型转换后等于 $b 则为 TRUE。
- $a === $b :如果 $a 等于 $b,并且它们的类型相同,则为 TRUE。
- $a != $b :如果 $a 在类型转换后不等于 $b,则为 TRUE。
- $a <> $b :如果在类型转换后 $a 不等于 $b 则为 TRUE。
- $a !== $b :如果 $a 不等于 $b,或者它们的类型不同,则为 TRUE。
- $a < $b:如果 $a 严格小于 $b,则为 TRUE。
- $a > $b:如果 $a 严格大于 $b,则为 TRUE。
- $a <= $b:如果 $a 小于或等于 $b,则为 TRUE。
- $a >= $b:如果 $a 大于或等于 $b,则为 TRUE。
- $a <=> $b:当 $a 分别小于、等于或大于 $b 时,小于、等于或大于零的整数。从 PHP 7 开始可用。
- $a ? $b : $c: if $a return $b else return $c (三元运算符)
- $a ?? $c:与 $a 相同?$a : $c (空合并运算符- 需要 PHP>=7)
Arithmetic Operators:
算术运算符:
- -$a: Opposite of $a.
- $a + $b: Sum of $a and $b.
- $a - $b: Difference of $a and $b.
- $a * $b: Product of $a and $b.
- $a / $b: Quotient of $a and $b.
- $a % $b: Remainder of $a divided by $b.
- $a ** $b: Result of raising $a to the $b'th power (introduced in PHP 5.6)
- -$a:与 $a 相反。
- $a + $b: $a 和 $b 的总和。
- $a - $b: $a 和 $b 的差异。
- $a * $b: $a 和 $b 的乘积。
- $a / $b: $a 和 $b 的商。
- $a % $b:$a 的余数除以 $b。
- $a ** $b:将 $a 提高到 $b 次方的结果(在 PHP 5.6 中引入)
Incrementing/Decrementing Operators:
递增/递减运算符:
- ++$a: Increments $a by one, then returns $a.
- $a++: Returns $a, then increments $a by one.
- --$a: Decrements $a by one, then returns $a.
- $a--: Returns $a, then decrements $a by one.
- ++$a:将 $a 增加 1,然后返回 $a。
- $a++:返回 $a,然后将 $a 加一。
- --$a: $a减一,然后返回 $a。
- $a--:返回 $a,然后将 $a 减一。
Bitwise Operators:
位运算符:
- $a & $b: Bits that are set in both $a and $b are set.
- $a | $b: Bits that are set in either $a or $b are set.
- $a ^ $b: Bits that are set in $a or $b but not both are set.
- ~ $a: Bits that are set in $a are not set, and vice versa.
- $a << $b: Shift the bits of $a $b steps to the left (each step means "multiply by two")
- $a >> $b: Shift the bits of $a $b steps to the right (each step means "divide by two")
- $a & $b:设置在 $a 和 $b 中的位。
- $a | $b:设置在 $a 或 $b 中的位。
- $a ^ $b:在 $a 或 $b 中设置但不能同时设置的位。
- ~ $a:在 $a 中设置的位未设置,反之亦然。
- $a << $b:将 $a $b 步骤的位向左移动(每一步表示“乘以二”)
- $a >> $b:将 $a $b 步骤的位向右移动(每一步表示“除以二”)
String Operators:
字符串运算符:
- $a . $b: Concatenation of $a and $b.
- $a 。$b: $a 和 $b 的串联。
Array Operators:
数组运算符:
- $a + $b: Union of $a and $b.
- $a == $b: TRUE if $a and $b have the same key/value pairs.
- $a === $b: TRUE if $a and $b have the same key/value pairs in the same order and of the same types.
- $a != $b: TRUE if $a is not equal to $b.
- $a <> $b: TRUE if $a is not equal to $b.
- $a !== $b: TRUE if $a is not identical to $b.
- $a + $b:$a 和 $b 的联合。
- $a == $b:如果 $a 和 $b 具有相同的键/值对,则为 TRUE。
- $a === $b:如果 $a 和 $b 具有相同顺序和相同类型的相同键/值对,则为 TRUE。
- $a != $b: 如果 $a 不等于 $b,则为 TRUE。
- $a <> $b:如果 $a 不等于 $b,则为 TRUE。
- $a !== $b:如果 $a 与 $b 不同则为真。
Assignment Operators:
赋值运算符:
- $a = $b: The value of $b is assigned to $a
- $a += $b: Same as $a = $a + $b
- $a -= $b: Same as $a = $a - $b
- $a *= $b: Same as $a = $a * $b
- $a /= $b: Same as $a = $a / $b
- $a %= $b: Same as $a = $a % $b
- $a **= $b: Same as $a = $a ** $b
- $a .= $b: Same as $a = $a . $b
- $a &= $b: Same as $a = $a & $b
- $a |= $b: Same as $a = $a | $b
- $a ^= $b: Same as $a = $a ^ $b
- $a <<= $b: Same as $a = $a << $b
- $a >>= $b: Same as $a = $a >> $b
- $a = $b: $b的值赋给 $a
- $a += $b:与 $a = $a + $b 相同
- $a -= $b:与 $a = $a - $b 相同
- $a *= $b:与 $a = $a * $b 相同
- $a /= $b:与 $a = $a / $b 相同
- $a %= $b:与 $a = $a % $b 相同
- $a **= $b:与 $a = $a ** $b 相同
- $a .= $b:与 $a = $a 相同。$b
- $a &= $b:与 $a = $a & $b 相同
- $a |= $b:与 $a = $a | 相同 $b
- $a ^= $b:与 $a = $a ^ $b 相同
- $a <<= $b:与 $a = $a << $b 相同
- $a >>= $b:与 $a = $a >> $b 相同
Note
笔记
and
operator and or
operator have lower precedence than assignment operator =
.
and
运算符和or
运算符的优先级低于赋值运算符=
。
This means that $a = true and false;
is equivalent to ($a = true) and false
.
这意味着$a = true and false;
相当于($a = true) and false
。
In most cases you will probably want to use &&
and ||
, which behave in a way known from languages like C, Java or JavaScript.
在大多数情况下,您可能想要使用&&
and ||
,它们的行为方式类似于 C、Java 或 JavaScript 等语言。
回答by rajangupta
Spaceship Operator <=>
(Added in PHP 7)
飞船操作员<=>
(在 PHP 7 中添加)
Examples for <=>
Spaceship operator (PHP 7, Source: PHP Manual):
<=>
Spaceship 操作符示例(PHP 7,来源:PHP 手册):
Integers, Floats, Strings, Arrays & objects for Three-way comparison of variables.
用于变量三路比较的整数、浮点数、字符串、数组和对象。
// 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
回答by mnv
{}
Curly braces
{}
大括号
- Blocks - curly braces/no curly braces?
- Curly braces in string in PHP
- PHP curly braces in array notation
And some words about last post
还有关于上一篇文章的一些话
$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