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.¹
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.
以前很难找到关于操作符和其他语法标记的问题。¹有链接到现有问题的主要思想是在堆栈溢出,所以它是容易引用它们,不要复制来自PHP手册的内容。
¹ Note: Since January 2013, Stack Overflow does support special characters. Just surround the search terms by quotes, e.g. [php] "==" vs "==="
¹注:2013年1月以来,堆栈溢出不支持特殊字符。用引号括住搜索词,例如[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 manual along 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 Operators or References
位运算符或引用
- What does it mean to start a PHP function with an ampersand?
- 用&符号启动PHP函数是什么意思?
- Understanding PHP & (ampersand, bitwise and) operator
- 理解PHP &(& &)运算符、位运算符和)运算符
- PHP "&" operator
- PHP运算符“&”
- Difference between & and && in PHP
- PHP中&和&的区别
- What does "&" mean here in PHP?
- 在PHP中,“&”是什么意思?
- What does "&" mean in this case?
- 在这种情况下,"&"是什么意思?
- What does the "&" sign mean in PHP?
- “&”符号在PHP中意味着什么?
- What does this signature mean (&) in PHP?
- 这个签名在PHP中是什么意思?
- How does the "&" operator work in a PHP function?
- “&”操作符在PHP函数中如何工作?
- What does & in &2 mean in PHP?
- & in &2在PHP中意味着什么?
- When should I use a bitwise operator?
- 什么时候使用位运算符?
- Is there ever a need to use ampersand in front of an object? (&$)
- 是否需要在对象前面使用&号?(& $)
=&
References
= &引用
- Reference assignment operator in PHP, =&
- PHP中的引用赋值操作符,=&
- What do the "=&" and "&=" operators in PHP mean?
- PHP中的“=&”和“&=”操作符是什么意思?
- What do the '&=' and '=&' operators do?
- '& '和'= '操作符是做什么的?
- What does =& mean in PHP?
- 在PHP中=&是什么意思?
& =按位运算符
- What do the "=&" and "&=" operators in PHP mean?
- PHP中的“=&”和“&=”操作符是什么意思?
- What do the '&=' and '=&' operators do?
- '& '和'= '操作符是做什么的?
逻辑运算符& &
- 'AND' vs '&&' as operator
- 和‘vs’和‘作为操作员’。
- Difference between & and && in PHP
- PHP中&和&的区别
- Is there any difference between "and" and "&&" operators in PHP?
- PHP中的“和”和“&”操作符之间有什么区别吗?
- PHP - and / or keywords
- PHP和/或关键字
%算术运算符
- What does the percent sign mean in PHP?
- PHP中的百分号是什么意思?
- What is the PHP operator % and how do I use it in real-world examples?
- PHP操作符%是什么?如何在实际示例中使用它?
! !逻辑运算符
- Double not (!!) operator in PHP
- 在PHP中加倍不(!!)运算符
@误差控制操作符
- What is the use of the @ symbol in PHP?
- @符号在PHP中有什么用?
- 'At' symbol before variable name in PHP: @$_POST
- 在PHP变量名之前的符号:@$_POST
- PHP functions and @functions
- PHP函数和@functions
- Should I use @ in my PHP code?
- 我应该在PHP代码中使用@吗?
:三元运算符
- What are the PHP operators "?" and ":" called and what do they do?
- PHP操作符是什么?
- ?: operator (the 'Elvis operator') in PHP
- ?: PHP中的操作符(“Elvis操作符”)
- Where can I read about conditionals done with "?" and ":" (colon)?
- 我在哪里可以读到用“?”和“:(冒号)做的条件句?
- Using PHP 5.3 ?: operator
- 使用PHP 5.3 ?:操作符
??
Null Coalesce Operator (since PHP 7)
? ?零合并运算符(从PHP 7开始)
- C#'s null coalescing operator (??) in PHP
- c#的空合并运算符(??)在PHP中
:
Alternative syntax for control structures, Ternary Operator
:控制结构的替代语法,三元运算符
- What is ":" in PHP?
- PHP中的“:”是什么?
- What does ":" mean in PHP?
- “:”在PHP中是什么意思?
::范围解析操作符
- What do two colons mean in PHP?
- 在PHP中,两个冒号是什么意思?
- What's the meaning of the PHP token name T_PAAMAYIM_NEKUDOTAYIM?
- PHP标记名T_PAAMAYIM_NEKUDOTAYIM的含义是什么?
- What's the difference between :: (double colon) and -> (arrow) in PHP?
- 在PHP中:(双冒号)和->(箭头)有什么区别?
- What exactly are late static bindings in PHP?
- PHP中的后期静态绑定到底是什么?
- static::staticFunctionName()
- 静态:staticFunctionName()
- Unexpected T_PAAMAYIM_NEKUDOTAYIM, expecting T_NS_Separator
- 意想不到的T_PAAMAYIM_NEKUDOTAYIM,期待T_NS_Separator
\名称空间
- Backslash in PHP -- what does it mean?
- PHP中的反斜杠是什么意思?
- What does a \ (backslash) do in PHP (5.3+)?
- PHP(5.3+)中\(反斜杠)的作用是什么?
- >类和对象
- What is the "->" PHP operator called and how do you say it when reading code out loud?
- 什么是“->”PHP操作符,当你大声读出代码时该怎么说?
- Where do we use the object operator "->" in PHP?
- 在PHP中,我们在哪里使用对象操作符“->”?
- What's the difference between :: (double colon) and -> (arrow) in PHP?
- 在PHP中:(双冒号)和->(箭头)有什么区别?
- What does the PHP syntax $var1->$var2 mean?
- PHP语法$var1->$var2是什么意思?
- What does "->" mean/refer to in PHP?
- “->”在PHP中是什么意思?
=>
Arrays
= >数组
- What does "=>" mean in PHP?
- 在PHP中“=>”是什么意思?
- Use of => in PHP
- 在PHP中使用=>
- What does $k => $v in foreach($ex as $k=>$v) mean?
- $k=>$v foreach($ex as $k=>$v)是什么意思?
^按位运算符
- How does the bitwise operator XOR ('^') work?
- 位操作符如何XOR(^)工作?
- What does ^ mean in PHP?
- ^是什么意思在PHP中?
> >按位运算符
- What does >> mean in PHP?
- >>在PHP中意味着什么?
< <逐位运算符< p>
- Strange print behaviour in PHP?
- PHP中奇怪的打印行为?
< < < Heredoc或Nowdoc
- What does <<<END mean in PHP?
-
在PHP中<
是什么意思? - PHP expression <<<EOB
- < < <区块php表达式< li>
- In PHP, what does "<<<" represent?
- 在PHP中,“<<<”表示什么?
- Using <<<CON in PHP
- 在PHP中使用< < <反对< li>
- What's this kind of syntax in PHP?
- PHP中的这种语法是什么?
=赋值操作符
- The 3 different equals
- 3种不同的=
= =比较运算符
- How do the PHP equality (== double equals) and identity (=== triple equals) comparison operators differ?
- PHP等式(=== double equals)和identity (=== = triple equals)的比较操作符有何不同?
- PHP != and == operators
- PHP !=和==操作符
- The 3 different equals
- 3种不同的=
- Type-juggling and (strict) greater/lesser-than comparisons in PHP
- PHP中的类型杂耍和(严格的)较大/较小的比较
= = =比较运算符
- What does "===" mean?
- " = = = "是什么意思?
- How do the PHP equality (== double equals) and identity (=== triple equals) comparison operators differ?
- PHP等式(=== double equals)和identity (=== = triple equals)的比较操作符有何不同?
- The 3 different equals
- 3种不同的=
- Type-juggling and (strict) greater/lesser-than comparisons in PHP
- PHP中的类型杂耍和(严格的)较大/较小的比较
! = =比较运算符
- What does !== comparison operator in PHP mean?
- PHP中的比较运算符是什么意思?
- Is there a difference between !== and != in PHP?
- 在PHP中是否有区别?
! =比较运算符
- PHP != and == operators
- PHP !=和==操作符
- Is there a difference between !== and != in PHP?
- 在PHP中是否有区别?
- comparing, !== versus !=
- 比较! = =和! =
- What is the difference between <> and !=
- <>和!=的区别是什么
< >比较运算符
- PHP operator <>
- PHP操作符< >
- PHP's <> operator
- PHP的< >操作符
- What is the difference between <> and !=
- <>和!=的区别是什么
- Type-juggling and (strict) greater/lesser-than comparisons in PHP
- PHP中的类型杂耍和(严格的)较大/较小的比较
<=>
Comparison Operators (since PHP 7.0)
<=>比较操作符(从PHP 7.0开始)
- Spaceship (three way comparison) operator
- 宇宙飞船(三路比较)操作员
|按位运算符
- 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
- PHP和/或关键字
- What exactly does || mean?
- ||到底是什么意思?
- The behaviour of the or operator in PHP
- PHP中or操作符的行为
~逐位运算符
- What does this ~ operator mean here?
- 这里的运算符是什么意思?
+
Arithmetic Operators, Array Operators
+算术运算符,数组运算符
- + operator for array in PHP?
- + PHP数组操作符?
+=
and -=
Assignment Operators
+=和-=赋值操作符
- What is += used for?
- +=用于什么?
- What does `$page -= 1` in my PHP document mean?
- PHP文档中的$page -= 1是什么意思?
++
and --
Incrementing/Decrementing Operators
++ -递增/递减运算符
- Understanding Incrementing
- 理解递增
- Answer below
- 下面的回答
=赋值操作符。
- What is the difference between .= and += in PHP?
- 在PHP中。=和+=的区别是什么?
- To understand a line of PHP
- 要理解一行PHP
。字符串运算符
- Difference between period and comma when concatenating with echo versus return?
- 当与echo和return连接时,句点和逗号之间的区别?
- What does a . (dot) do in PHP?
- 是什么。在PHP(点)做什么?
,函数参数
- Difference between period and comma when concatenating with echo versus return?
- 当与echo和return连接时,句点和逗号之间的区别?
,变量声明
- What do commas mean in a variable declaration?
- 变量声明中的逗号是什么意思?
变量$ $变量
- What does $$ (dollar dollar or double dollar) mean in PHP?
- $(美元或双倍美元)在PHP中意味着什么?
- what is "$$" in PHP
- PHP中的“$$”是什么
- $function() and $$variable
- $函数()和$ $变量中
“执行操作
<?=
Short Open Tags
< ?=短开标签
- What does this symbol mean in PHP <?=
- 这个符号在PHP
- What does '<?=' mean in PHP?
- ' < ?= '在PHP中意味着什么?
- What does <?= mean?
- <什么?=是什么意思?< li>
[]
Arrays (short syntax since PHP 5.4)
[]数组(PHP 5.4以来的简短语法)
- PHP arrays... What is/are the meaning(s) of an empty bracket?
- PHP数组…空括号的含义是什么?
- What is the meaning of []
- []的意义是什么
- Php array_push() vs myArray[]
- Php array_push()vs myArray[]
- What does [] mean when reading from a PHP array?
- 从PHP数组中读取[]意味着什么?
- Shorthand for arrays: literal
$var = []
empty array - 数组的简写:$var =[]空数组
< ?打开和关闭标签
- Are PHP short tags acceptable to use?
- 可以使用PHP短标记吗?
...
Argument unpacking (since PHP 5.6)
…参数解包(从PHP 5.6开始)
**
Exponentiation (since PHP 5.6)
**指数(PHP 5.6)
#
One-line shell-style comment
#一行shell风格的评论
- Can I use hashes for comments in PHP?
- 我可以在PHP中使用哈希的注释吗?
18 个解决方案
#1
928
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 first then the result is returned. If put after the variable, the variable is first returned, 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 then you want to subtract one from it.
但是,必须使用$apple,因为首先要显示当前的苹果数量,然后要从中减去1。
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字符(a-z和a-z)。
Stack Overflow Posts:
堆栈溢出的帖子:
#2
353
Bitwise Operator
What is a bit? A bit is a representation of 1 or 0. Basically OFF(0) and ON(1)
一点是什么?位是1或0的表示。基本上从(0)和(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,这意味着每一个位都被设置。
-------------------------------------------
| 1 Byte ( 8 bits ) |
-------------------------------------------
|Place Value | 128| 64| 32| 16| 8| 4| 2| 1|
-------------------------------------------
This representation of 1 Byte
这个1字节的表示
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.
你会注意到我们有3位,在8 2 1列。把这些加起来:8 + 2 + 1 = 11。
#3
216
_别名gettext()
The underscore character '_' as in _()
is an alias to the gettext()
function.
__()中的下划线字符'_'是gettext()函数的别名。
#4
201
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
a . b Concatenation Concatenate two strings: "Hi" . "Ha"
#5
190
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__:文件的完整路径和文件名。如果在include中使用,则返回包含的文件的名称。由于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__在特性方面也有作用。当在特征方法中使用时,__CLASS__是特征所使用的类的名称。
__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中添加的)中定义的。
源
#6
184
<=>
Spaceship Operator
Added in PHP 7
The spaceship operator <=>
is the latest comparison operator added in PHP 7. It is a non-associative binary 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 - 当两个操作数相等时为0
- Less than
0
when the left-hand operand is less than the right-hand operand - 当左操作数小于右操作数时,小于0
- Greater than
0
when the left-hand operand is greater than the right-hand operand - 当左操作数大于右操作数时大于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...
$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...
$arr = [4,2,1,3];
usort($arr, function ($a, $b) {
return $a <=> $b;
});
#7
118
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)
#8
92
An overview of operators in PHP:
Logical Operators:
- $a && $b : TRUE if both $a and $b are TRUE.
- $a和$b:如果$a和$b都是正确的。
- $a || $b : TRUE if either $a or $b is TRUE.
- $ || $b:如果a $或b $都是正确的。
- $a xor $b : TRUE if either $a or $b is TRUE, but not both.
- $a x $b:如果$a或$b是正确的,但不是两者都是正确的。
- ! $a : TRUE if $a is not TRUE.
- !$a:如果$a不是真的。
- $a and $b : TRUE if both $a and $b are TRUE.
- $a和$b:如果$a和$b都是正确的。
- $a or $b : TRUE if either $a or $b is TRUE.
- $a或$b:如果$a或$b是正确的。
Comparison operators:
- $a == $b : TRUE if $a is equal to $b after type juggling.
- $a == $b:如果$a等于$b。
- $a === $b : TRUE if $a is equal to $b, and they are of the same type.
- $a === $b:如果$a = $b,它们属于同一类型,则为真。
- $a != $b : TRUE if $a is not equal to $b after type juggling.
- $a != $b:如果$a不等于$b,那是真的。
- $a <> $b : TRUE if $a is not equal to $b after type juggling.
- $a <> $b:如果$a在类型杂耍之后不等于$b。
- $a !== $b : TRUE if $a is not equal to $b, or they are not of the same type.
- $a !== $b:如果$a不等于$b,或它们不属于同一类型。
- $a < $b : TRUE if $a is strictly less than $b.
- $a < $b:如果$a严格小于$b,则为真。
- $a > $b : TRUE if $a is strictly greater than $b.
- $a > $b:确实,如果$a严格大于$b。
- $a <= $b : TRUE if $a is less than or equal to $b.
- $a <= $b:如果$a小于或等于$b,则为真。
- $a >= $b : TRUE if $a is greater than or equal to $b.
- $a >= $b:如果$a大于或等于$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:当$a分别小于、等于或大于b时小于、等于或大于零的整数。PHP 7可用。
- $a ? $b : $c : if $a return $b else return $c (ternary operator)
- 一美元?$b: $c:如果$a返回$b返回$c(三元运营商)
- $a ?? $c : Same as $a ? $a : $c (null coalescing operator - requires PHP>=7)
- 一美元? ?$c:和$a一样吗?$a: $c (null coalescing操作符——要求PHP>=7)
Arithmetic Operators:
- -$a : Opposite of $a.
- -$a:与$a相对。
- $a + $b : Sum of $a and $b.
- $a + $b: $a和$b的总和。
- $a - $b : Difference of $a and $b.
- $a - $b: $a和$b的差值。
- $a * $b : Product of $a and $b.
- $a * $b: $a和$b的产品。
- $a / $b : Quotient of $a and $b.
- $a / $b: $a和$b的商。
- $a % $b : Remainder of $a divided by $b.
- $a % $b:剩余的$a除以$b。
- $a ** $b : Result of raising $a to the $b'th power (introduced in PHP 5.6)
- $a * $b:将$a提升到$b次方的结果(PHP 5.6中引入)
Incrementing/Decrementing Operators:
- ++$a : Increments $a by one, then returns $a.
- ++$a:增加$a一个,然后返回$a。
- $a++ : Returns $a, then increments $a by one.
- $a++:返回$a,然后递增$a。
- --$a : Decrements $a by one, then returns $a.
- ——美元:美元的精神性,然后返回一个美元。
- $a-- : Returns $a, then decrements $a by one.
- $a-:返回$a,然后递减$a。
Bitwise Operators:
- $a & $b : Bits that are set in both $a and $b are set.
- $a & $b:设置在$a和$b中的位。
- $a | $b : Bits that are set in either $a or $b are set.
- $a | $b:设置为$a或$b的位元。
- $a ^ $b : Bits that are set in $a or $b but not both are set.
- $ ^ $ b:部分设置在a或b美元但不都是集。
- ~ $a : Bits that are set in $a are not set, and vice versa.
- ~ $a: $a中设置的位没有设置,反之亦然。
- $a << $b : Shift the bits of $a $b steps to the left (each step means "multiply by two")
- $a < $b:将$a $b步骤的位移到左边(每一步表示“乘以2”)
- $a >> $b : Shift the bits of $a $b steps to the right (each step means "divide by two")
- $a >> $b:将$a $b步骤向右移动(每一步表示“除2”)
String Operators:
- $a . $b : Concatenation of $a and $b.
- 一个美元。$b: $a和$b的串联。
Array Operators:
- $a + $b : Union of $a and $b.
- $a + $b: $a和$b的联合。
- $a == $b : TRUE if $a and $b have the same key/value pairs.
- $a == $b:如果$a和$b拥有相同的密钥/值对,则为真。
- $a === $b : TRUE if $a and $b have the same key/value pairs in the same order and of the same types.
- $a === = $b:如果$a和$b的键/值对顺序相同,类型相同,则为真。
- $a != $b : TRUE if $a is not equal to $b.
- $a = $b:如果$a不等于$b,这是真的。
- $a <> $b : TRUE if $a is not equal to $b.
- $a <> $b:如果$a不等于$b,则为真。
- $a !== $b : TRUE if $a is not identical to $b.
- $a == $b:如果$a与$b不相同,则为真。
Assignment Operators:
- $a = $b : The value of $b is assigned to $a
- $a = $b: $b的价值被分配给$a
- $a += $b : Same as $a = $a + $b
- $a += $b:与$a = $a + $b相同
- $a -= $b : Same as $a = $a - $b
- $a -= $b:与$a = $a - $b相同
- $a *= $b : Same as $a = $a * $b
- $ * = $ b:a = * $美元一样
- $a /= $b : Same as $a = $a / $b
- $a /= $b:与$a = $a / $b相同
- $a %= $b : Same as $a = $a % $b
- $a %= $b:与$a = $b相同。
- $a **= $b : Same as $a = $a ** $b
- $a *= $b:与$a = $a * $b相同
- $a .= $b : Same as $a = $a . $b
- $a .= $b:同$a = $a。b美元
- $a &= $b : Same as $a = $a & $b
- $ & = $ b:$ = $ & $ b一样
- $a |= $b : Same as $a = $a | $b
- $a |= $b:与$a = $a | $b相同
- $a ^= $b : Same as $a = $a ^ $b
- $ ^ = $ b:a = $ ^ $美元一样
- $a <<= $b : Same as $a = $a << $b
- $a <= $b:与$a = $a < $b相同
- $a >>= $b : Same as $a = $a >> $b
- $ >>= $b:与$a = $a >> $b相同
Note
and
operator and or
operator have lower precedence than assignment operator =
.
运算符和或运算符的优先级低于赋值运算符=。
This means that $a = true and false;
is equivalent to ($a = true) and false
.
这意味着$a = true和false;等于($a = true)和false。
In most cases you will probably want to use &&
and ||
, which behave in a way known from languages like C, Java or JavaScript.
在大多数情况下,您可能希望使用&和||,它们的行为方式可以从C、Java或JavaScript等语言中了解。
#9
70
Spaceship Operator <=>
(Added in PHP 7)
Examples for <=>
Spaceship operator (PHP 7, Source: PHP Manual):
<=>太空船操作员示例(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
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
#10
53
{}
Curly braces
花括号{ }
- Blocks - curly braces/no curly braces?
- 大括号-大括号/没有大括号?
- Curly braces in string in PHP
- PHP中字符串中的花括号
- PHP curly braces in array notation
- 在数组符号中使用PHP花括号
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
#11
43
PHP Strings: PHP Strings can be specified in four ways not just two ways:
PHP字符串:PHP字符串可以以四种方式指定,而不仅仅是两种方式:
1) Single Quote Strings:
1)单引号字符串:
$string = 'This is my string'; // print This is my string
2) Double Quote Strings:
2)双引号字符串:
$str = 'string';
$string = "This is my $str"; // print This is my string
3) Heredoc:
3)Heredoc:
$string = <<<EOD
This is my string
EOD; // print This is my string
4) Nowdoc (since PHP 5.3.0):
4)Nowdoc(自PHP 5.3.0):
$string = <<<'END_OF_STRING'
This is my string
END_OF_STRING; // print This is my string
#12
38
Null coalescing operator (??)
空合并操作符(? ?)
This operator has been added in PHP 7.0 for the common case of needing to use a ternary operator in conjunction with isset()
. It returns its first operand if it exists and is not NULL
; otherwise it returns its second operand.
对于需要使用三元操作符与isset()的常见情况,该操作符已添加到PHP 7.0中。如果它存在且不为空,则返回它的第一个操作数;否则它返回第二个操作数。
<?php
// Fetches the value of $_GET['user'] and returns 'nobody'
// if it does not exist.
$username = $_GET['user'] ?? 'nobody';
// This is equivalent to:
$username = isset($_GET['user']) ? $_GET['user'] : 'nobody';
// Coalescing can be chained: this will return the first
// defined value out of $_GET['user'], $_POST['user'], and
// 'nobody'.
$username = $_GET['user'] ?? $_POST['user'] ?? 'nobody';
?>
#13
29
QUESTION:
问题:
What does =>
mean?
= >是什么意思?
ANSWER:
答:
=>
Is the symbol we humans decided to use to separate "Key" => "Value"
pairs in Associative Arrays.
=>是我们人类决定用来分离“键”=>“值”对关联数组的符号。
ELABORATING:
详细说明:
To understand this, we have to know what Associative Arrays are. The first thing that comes up when a conventional programmer thinks of an array (in PHP) would be something similar to:
要理解这一点,我们必须知道关联数组是什么。当一个传统的程序员想到一个数组(在PHP中)时,首先会想到的是:
$myArray1 = array(2016, "hello", 33);//option 1
$myArray2 = [2016, "hello", 33];//option 2
$myArray3 = [];//option 3
$myArray3[] = 2016;
$myArray3[] = "hello";
$myArray3[] = 33;
Where as, if we wanted to call the array in some later part of the code, we could do:
如果我们想在后面的代码中调用数组,我们可以这样做:
echo $myArray1[1];// output: hello
echo $myArray2[1];// output: hello
echo $myArray3[1];// output: hello
So far so good. However, as humans, we might find it hard to remember that index [0]
of the array is the value of the year 2016, index [1]
of the array is a greetings, and index [2]
of the array is a simple integer value. The alternative we would then have is to use what is called an Associative Array. An Associative array has a few differences from a Sequential Array (which is what the previous cases were since they increment the index used in a predetermined sequence, by incrementing by 1 for each following value).
目前为止一切都很顺利。但是,作为人类,我们可能很难记住数组的索引[0]是2016年的值,数组的索引[1]是问候语,数组的索引[2]是一个简单的整数值。我们可以选择使用所谓的关联数组。关联数组与序列数组有一些不同之处(这是前面的情况,因为它们对预定序列中使用的索引进行递增,每个值递增1)。
Differences (between a sequential and associative array):
差异(序列数组和关联数组之间的差异):
-
Durring the declaration of an Associative Array, you don't only include the
value
of what you want to put in the array, but you also put the index value (called thekey
) which you want to use when calling the array in later parts of the code. The following syntax is used during it's declaration:"key" => "value"
.在关联数组的声明中,您不仅包含要放入数组的值,而且还将索引值(称为键)放在代码的后面调用数组时使用。在声明时使用以下语法:“key”=>“value”。
-
When using the Associative Array, the
key
value would then be placed inside the index of the array to retrieve the desiredvalue
.当使用关联数组时,键值将被放置在数组的索引中,以检索所需的值。
For instance:
例如:
$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;
And now, to receive the same output as before, the key
value would be used in the arrays index:
现在,要得到和以前一样的输出,键值将被用于数组索引:
echo $myArray1["Greetings"];// output: hello
echo $myArray2["Greetings"];// output: hello
echo $myArray3["Greetings"];// output: hello
FINAL POINT:
最后一点:
So from the above example, it is pretty easy to see that the =>
symbol is used to express the relationship of an Associative Array between each of the key
and value
pairs in an array DURING the initiation of the values within the array.
因此,从上面的示例中,很容易看到=>符号用于表示数组中每个键和值对之间的关联数组在数组中值初始化期间的关系。
#14
18
Question:
问题:
What does "&" mean here in PHP?
在PHP中,“&”是什么意思?
PHP "&" operator
Makes life more easier once we get used to it..(check example below carefully)
一旦我们习惯了,生活会更容易。(仔细检查在下面的例子)
& usually checks bits that are set in both $a and $b are set.
通常检查a $和b $设置的位。
have you even noticed how these calls works?
你有没有注意到这些调用是如何工作的?
error_reporting(E_ERROR | E_WARNING | E_PARSE);
error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE);
error_reporting(E_ALL & ~E_NOTICE);
error_reporting(E_ALL);
So behind all above is game of bitwise operator and bits.
所以在所有这些后面都是位运算符和位元的游戏。
One usefull case of these is easy configurations like give below, so a single integer field can store thousands of combos for you.
其中一个有用的例子是如下所示的简单配置,因此一个整数字段可以为您存储数千个组合。
Most people have already read the docs but didn't reliase the real world use case of these bitwise operators.
大多数人已经阅读了文档,但是没有重新使用这些位操作符的真实世界用例。
Example That you 'll love
<?php
class Config {
// our constants must be 1,2,4,8,16,32,64 ....so on
const TYPE_CAT=1;
const TYPE_DOG=2;
const TYPE_LION=4;
const TYPE_RAT=8;
const TYPE_BIRD=16;
const TYPE_ALL=31;
private $config;
public function __construct($config){
$this->config=$config;
if($this->is(Config::TYPE_CAT)){
echo 'cat ';
}
if($this->is(Config::TYPE_DOG)){
echo 'dog ';
}
if($this->is(Config::TYPE_RAT)){
echo 'rat ';
}
if($this->is(Config::TYPE_LION)){
echo 'lion ';
}
if($this->is(Config::TYPE_BIRD)){
echo 'bird ';
}
echo "\n";
}
private function is($value){
return $this->config & $value;
}
}
new Config(Config::TYPE_ALL);
// cat dog rat lion bird
new Config(Config::TYPE_BIRD);
//bird
new Config(Config::TYPE_BIRD | Config::TYPE_DOG);
//dog bird
new Config(Config::TYPE_ALL & ~Config::TYPE_DOG & ~Config::TYPE_CAT);
//rat lion bird
#15
15
Null Coalesce Operator php
零合并算子php
The null coalescing operator (??) has been added to PHP7 for the common case of needing to use a ternary in conjunction with isset(). It returns its first operand if it exists and is not NULL otherwise it returns its second operand, such as following example:
对于需要与isset()一起使用三元组的常见情况,PHP7中添加了null coalescing运算符(?)。它返回它的第一个操作数,如果它存在并不是NULL,否则它将返回第二个操作数,例如下面的例子:
$username = $_GET['user'] ?? 'nobody';
// This is equivalent to:
$username = isset($_GET['user']) ? $_GET['user'] : 'nobody';
#16
14
Null Coalesce operator "??" (Added in PHP 7)
Not the catchiest name for an operator, but PHP 7 brings in the rather handy null coalesce so I thought I'd share an example.
对于操作符来说,这并不是最容易理解的名称,但是PHP 7引入了非常方便的空合并,所以我想我应该分享一个示例。
In PHP 5, we already have a ternary operator, which tests a value, and then returns the second element if that returns true and the third if it doesn't:
在PHP 5中,我们已经有一个三元运算符,它测试一个值,然后返回第二个元素(如果返回真),返回第三个元素(如果返回真):
echo $count ? $count : 10; // outputs 10
There is also a shorthand for that which allows you to skip the second element if it's the same as the first one: echo $count ?: 10; // also outputs 10
如果第二个元素与第一个元素相同,您也可以略过第二个元素:echo $count ?/ /输出10
In PHP 7 we additionally get the ?? operator which rather than indicating extreme confusion which is how I would usually use two question marks together instead allows us to chain together a string of values. Reading from left to right, the first value which exists and is not null is the value that will be returned.
在PHP 7中,我们还得到了??运算符,它不是表示极端的混乱,也就是我通常把两个问号放在一起使用,而是允许我们将一系列的值链接在一起。从左到右读取,存在的第一个值并不是null,是返回的值。
// $a is not set
$b = 16;
echo $a ?? 2; // outputs 2
echo $a ?? $b ?? 7; // outputs 16
This construct is useful for giving priority to one or more values coming perhaps from user input or existing configuration, and safely falling back on a given default if that configuration is missing. It's kind of a small feature but it's one that I know I'll be using as soon as my applications upgrade to PHP 7.
这个构造对于优先考虑来自用户输入或现有配置的一个或多个值是很有用的,如果缺少这个配置,则可以安全地返回到给定的默认值。这是一个小功能,但我知道我的应用程序升级到PHP 7时,我就会用到它。
#17
12
==
is used for check equality without considering variable data-type
==在不考虑变量数据类型的情况下使用。
===
is used for check equality for both the variable value* and **data-type
===用于检查变量值*和**数据类型的相等性
Example
$a = 5
一美元= 5
-
if ($a == 5)
- will evaluate to trueif ($a == 5) -将计算为true
-
if ($a == '5')
- will evaluate to true, because while comparing this both value php internally convert that string value into integer and then compare both valuesif ($a = '5') -将计算为true,因为在比较这个值时,两个值php都在内部将字符串值转换为integer,然后比较两个值
-
if ($a === 5)
- will evaluate to true如果($a === 5) -将计算为true
-
if ($a === '5')
- will evaluate to false, because value is 5, but this value 5 is not an integer.if ($a === '5') -将计算为false,因为值是5,但是这个值5不是整数。
#18
11
Here's the names of all the PHP operators (TOKEN).
这里是所有PHP操作符(令牌)的名称。
Reference http://php.net/manual/en/tokens.php
参考http://php.net/manual/en/tokens.php
#1
928
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 first then the result is returned. If put after the variable, the variable is first returned, 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 then you want to subtract one from it.
但是,必须使用$apple,因为首先要显示当前的苹果数量,然后要从中减去1。
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字符(a-z和a-z)。
Stack Overflow Posts:
堆栈溢出的帖子:
#2
353
Bitwise Operator
What is a bit? A bit is a representation of 1 or 0. Basically OFF(0) and ON(1)
一点是什么?位是1或0的表示。基本上从(0)和(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,这意味着每一个位都被设置。
-------------------------------------------
| 1 Byte ( 8 bits ) |
-------------------------------------------
|Place Value | 128| 64| 32| 16| 8| 4| 2| 1|
-------------------------------------------
This representation of 1 Byte
这个1字节的表示
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.
你会注意到我们有3位,在8 2 1列。把这些加起来:8 + 2 + 1 = 11。
#3
216
_别名gettext()
The underscore character '_' as in _()
is an alias to the gettext()
function.
__()中的下划线字符'_'是gettext()函数的别名。
#4
201
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
a . b Concatenation Concatenate two strings: "Hi" . "Ha"
#5
190
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__:文件的完整路径和文件名。如果在include中使用,则返回包含的文件的名称。由于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__在特性方面也有作用。当在特征方法中使用时,__CLASS__是特征所使用的类的名称。
__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中添加的)中定义的。
源
#6
184
<=>
Spaceship Operator
Added in PHP 7
The spaceship operator <=>
is the latest comparison operator added in PHP 7. It is a non-associative binary 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 - 当两个操作数相等时为0
- Less than
0
when the left-hand operand is less than the right-hand operand - 当左操作数小于右操作数时,小于0
- Greater than
0
when the left-hand operand is greater than the right-hand operand - 当左操作数大于右操作数时大于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...
$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...
$arr = [4,2,1,3];
usort($arr, function ($a, $b) {
return $a <=> $b;
});
#7
118
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)
#8
92
An overview of operators in PHP:
Logical Operators:
- $a && $b : TRUE if both $a and $b are TRUE.
- $a和$b:如果$a和$b都是正确的。
- $a || $b : TRUE if either $a or $b is TRUE.
- $ || $b:如果a $或b $都是正确的。
- $a xor $b : TRUE if either $a or $b is TRUE, but not both.
- $a x $b:如果$a或$b是正确的,但不是两者都是正确的。
- ! $a : TRUE if $a is not TRUE.
- !$a:如果$a不是真的。
- $a and $b : TRUE if both $a and $b are TRUE.
- $a和$b:如果$a和$b都是正确的。
- $a or $b : TRUE if either $a or $b is TRUE.
- $a或$b:如果$a或$b是正确的。
Comparison operators:
- $a == $b : TRUE if $a is equal to $b after type juggling.
- $a == $b:如果$a等于$b。
- $a === $b : TRUE if $a is equal to $b, and they are of the same type.
- $a === $b:如果$a = $b,它们属于同一类型,则为真。
- $a != $b : TRUE if $a is not equal to $b after type juggling.
- $a != $b:如果$a不等于$b,那是真的。
- $a <> $b : TRUE if $a is not equal to $b after type juggling.
- $a <> $b:如果$a在类型杂耍之后不等于$b。
- $a !== $b : TRUE if $a is not equal to $b, or they are not of the same type.
- $a !== $b:如果$a不等于$b,或它们不属于同一类型。
- $a < $b : TRUE if $a is strictly less than $b.
- $a < $b:如果$a严格小于$b,则为真。
- $a > $b : TRUE if $a is strictly greater than $b.
- $a > $b:确实,如果$a严格大于$b。
- $a <= $b : TRUE if $a is less than or equal to $b.
- $a <= $b:如果$a小于或等于$b,则为真。
- $a >= $b : TRUE if $a is greater than or equal to $b.
- $a >= $b:如果$a大于或等于$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:当$a分别小于、等于或大于b时小于、等于或大于零的整数。PHP 7可用。
- $a ? $b : $c : if $a return $b else return $c (ternary operator)
- 一美元?$b: $c:如果$a返回$b返回$c(三元运营商)
- $a ?? $c : Same as $a ? $a : $c (null coalescing operator - requires PHP>=7)
- 一美元? ?$c:和$a一样吗?$a: $c (null coalescing操作符——要求PHP>=7)
Arithmetic Operators:
- -$a : Opposite of $a.
- -$a:与$a相对。
- $a + $b : Sum of $a and $b.
- $a + $b: $a和$b的总和。
- $a - $b : Difference of $a and $b.
- $a - $b: $a和$b的差值。
- $a * $b : Product of $a and $b.
- $a * $b: $a和$b的产品。
- $a / $b : Quotient of $a and $b.
- $a / $b: $a和$b的商。
- $a % $b : Remainder of $a divided by $b.
- $a % $b:剩余的$a除以$b。
- $a ** $b : Result of raising $a to the $b'th power (introduced in PHP 5.6)
- $a * $b:将$a提升到$b次方的结果(PHP 5.6中引入)
Incrementing/Decrementing Operators:
- ++$a : Increments $a by one, then returns $a.
- ++$a:增加$a一个,然后返回$a。
- $a++ : Returns $a, then increments $a by one.
- $a++:返回$a,然后递增$a。
- --$a : Decrements $a by one, then returns $a.
- ——美元:美元的精神性,然后返回一个美元。
- $a-- : Returns $a, then decrements $a by one.
- $a-:返回$a,然后递减$a。
Bitwise Operators:
- $a & $b : Bits that are set in both $a and $b are set.
- $a & $b:设置在$a和$b中的位。
- $a | $b : Bits that are set in either $a or $b are set.
- $a | $b:设置为$a或$b的位元。
- $a ^ $b : Bits that are set in $a or $b but not both are set.
- $ ^ $ b:部分设置在a或b美元但不都是集。
- ~ $a : Bits that are set in $a are not set, and vice versa.
- ~ $a: $a中设置的位没有设置,反之亦然。
- $a << $b : Shift the bits of $a $b steps to the left (each step means "multiply by two")
- $a < $b:将$a $b步骤的位移到左边(每一步表示“乘以2”)
- $a >> $b : Shift the bits of $a $b steps to the right (each step means "divide by two")
- $a >> $b:将$a $b步骤向右移动(每一步表示“除2”)
String Operators:
- $a . $b : Concatenation of $a and $b.
- 一个美元。$b: $a和$b的串联。
Array Operators:
- $a + $b : Union of $a and $b.
- $a + $b: $a和$b的联合。
- $a == $b : TRUE if $a and $b have the same key/value pairs.
- $a == $b:如果$a和$b拥有相同的密钥/值对,则为真。
- $a === $b : TRUE if $a and $b have the same key/value pairs in the same order and of the same types.
- $a === = $b:如果$a和$b的键/值对顺序相同,类型相同,则为真。
- $a != $b : TRUE if $a is not equal to $b.
- $a = $b:如果$a不等于$b,这是真的。
- $a <> $b : TRUE if $a is not equal to $b.
- $a <> $b:如果$a不等于$b,则为真。
- $a !== $b : TRUE if $a is not identical to $b.
- $a == $b:如果$a与$b不相同,则为真。
Assignment Operators:
- $a = $b : The value of $b is assigned to $a
- $a = $b: $b的价值被分配给$a
- $a += $b : Same as $a = $a + $b
- $a += $b:与$a = $a + $b相同
- $a -= $b : Same as $a = $a - $b
- $a -= $b:与$a = $a - $b相同
- $a *= $b : Same as $a = $a * $b
- $ * = $ b:a = * $美元一样
- $a /= $b : Same as $a = $a / $b
- $a /= $b:与$a = $a / $b相同
- $a %= $b : Same as $a = $a % $b
- $a %= $b:与$a = $b相同。
- $a **= $b : Same as $a = $a ** $b
- $a *= $b:与$a = $a * $b相同
- $a .= $b : Same as $a = $a . $b
- $a .= $b:同$a = $a。b美元
- $a &= $b : Same as $a = $a & $b
- $ & = $ b:$ = $ & $ b一样
- $a |= $b : Same as $a = $a | $b
- $a |= $b:与$a = $a | $b相同
- $a ^= $b : Same as $a = $a ^ $b
- $ ^ = $ b:a = $ ^ $美元一样
- $a <<= $b : Same as $a = $a << $b
- $a <= $b:与$a = $a < $b相同
- $a >>= $b : Same as $a = $a >> $b
- $ >>= $b:与$a = $a >> $b相同
Note
and
operator and or
operator have lower precedence than assignment operator =
.
运算符和或运算符的优先级低于赋值运算符=。
This means that $a = true and false;
is equivalent to ($a = true) and false
.
这意味着$a = true和false;等于($a = true)和false。
In most cases you will probably want to use &&
and ||
, which behave in a way known from languages like C, Java or JavaScript.
在大多数情况下,您可能希望使用&和||,它们的行为方式可以从C、Java或JavaScript等语言中了解。
#9
70
Spaceship Operator <=>
(Added in PHP 7)
Examples for <=>
Spaceship operator (PHP 7, Source: PHP Manual):
<=>太空船操作员示例(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
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
#10
53
{}
Curly braces
花括号{ }
- Blocks - curly braces/no curly braces?
- 大括号-大括号/没有大括号?
- Curly braces in string in PHP
- PHP中字符串中的花括号
- PHP curly braces in array notation
- 在数组符号中使用PHP花括号
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
#11
43
PHP Strings: PHP Strings can be specified in four ways not just two ways:
PHP字符串:PHP字符串可以以四种方式指定,而不仅仅是两种方式:
1) Single Quote Strings:
1)单引号字符串:
$string = 'This is my string'; // print This is my string
2) Double Quote Strings:
2)双引号字符串:
$str = 'string';
$string = "This is my $str"; // print This is my string
3) Heredoc:
3)Heredoc:
$string = <<<EOD
This is my string
EOD; // print This is my string
4) Nowdoc (since PHP 5.3.0):
4)Nowdoc(自PHP 5.3.0):
$string = <<<'END_OF_STRING'
This is my string
END_OF_STRING; // print This is my string
#12
38
Null coalescing operator (??)
空合并操作符(? ?)
This operator has been added in PHP 7.0 for the common case of needing to use a ternary operator in conjunction with isset()
. It returns its first operand if it exists and is not NULL
; otherwise it returns its second operand.
对于需要使用三元操作符与isset()的常见情况,该操作符已添加到PHP 7.0中。如果它存在且不为空,则返回它的第一个操作数;否则它返回第二个操作数。
<?php
// Fetches the value of $_GET['user'] and returns 'nobody'
// if it does not exist.
$username = $_GET['user'] ?? 'nobody';
// This is equivalent to:
$username = isset($_GET['user']) ? $_GET['user'] : 'nobody';
// Coalescing can be chained: this will return the first
// defined value out of $_GET['user'], $_POST['user'], and
// 'nobody'.
$username = $_GET['user'] ?? $_POST['user'] ?? 'nobody';
?>
#13
29
QUESTION:
问题:
What does =>
mean?
= >是什么意思?
ANSWER:
答:
=>
Is the symbol we humans decided to use to separate "Key" => "Value"
pairs in Associative Arrays.
=>是我们人类决定用来分离“键”=>“值”对关联数组的符号。
ELABORATING:
详细说明:
To understand this, we have to know what Associative Arrays are. The first thing that comes up when a conventional programmer thinks of an array (in PHP) would be something similar to:
要理解这一点,我们必须知道关联数组是什么。当一个传统的程序员想到一个数组(在PHP中)时,首先会想到的是:
$myArray1 = array(2016, "hello", 33);//option 1
$myArray2 = [2016, "hello", 33];//option 2
$myArray3 = [];//option 3
$myArray3[] = 2016;
$myArray3[] = "hello";
$myArray3[] = 33;
Where as, if we wanted to call the array in some later part of the code, we could do:
如果我们想在后面的代码中调用数组,我们可以这样做:
echo $myArray1[1];// output: hello
echo $myArray2[1];// output: hello
echo $myArray3[1];// output: hello
So far so good. However, as humans, we might find it hard to remember that index [0]
of the array is the value of the year 2016, index [1]
of the array is a greetings, and index [2]
of the array is a simple integer value. The alternative we would then have is to use what is called an Associative Array. An Associative array has a few differences from a Sequential Array (which is what the previous cases were since they increment the index used in a predetermined sequence, by incrementing by 1 for each following value).
目前为止一切都很顺利。但是,作为人类,我们可能很难记住数组的索引[0]是2016年的值,数组的索引[1]是问候语,数组的索引[2]是一个简单的整数值。我们可以选择使用所谓的关联数组。关联数组与序列数组有一些不同之处(这是前面的情况,因为它们对预定序列中使用的索引进行递增,每个值递增1)。
Differences (between a sequential and associative array):
差异(序列数组和关联数组之间的差异):
-
Durring the declaration of an Associative Array, you don't only include the
value
of what you want to put in the array, but you also put the index value (called thekey
) which you want to use when calling the array in later parts of the code. The following syntax is used during it's declaration:"key" => "value"
.在关联数组的声明中,您不仅包含要放入数组的值,而且还将索引值(称为键)放在代码的后面调用数组时使用。在声明时使用以下语法:“key”=>“value”。
-
When using the Associative Array, the
key
value would then be placed inside the index of the array to retrieve the desiredvalue
.当使用关联数组时,键值将被放置在数组的索引中,以检索所需的值。
For instance:
例如:
$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;
And now, to receive the same output as before, the key
value would be used in the arrays index:
现在,要得到和以前一样的输出,键值将被用于数组索引:
echo $myArray1["Greetings"];// output: hello
echo $myArray2["Greetings"];// output: hello
echo $myArray3["Greetings"];// output: hello
FINAL POINT:
最后一点:
So from the above example, it is pretty easy to see that the =>
symbol is used to express the relationship of an Associative Array between each of the key
and value
pairs in an array DURING the initiation of the values within the array.
因此,从上面的示例中,很容易看到=>符号用于表示数组中每个键和值对之间的关联数组在数组中值初始化期间的关系。
#14
18
Question:
问题:
What does "&" mean here in PHP?
在PHP中,“&”是什么意思?
PHP "&" operator
Makes life more easier once we get used to it..(check example below carefully)
一旦我们习惯了,生活会更容易。(仔细检查在下面的例子)
& usually checks bits that are set in both $a and $b are set.
通常检查a $和b $设置的位。
have you even noticed how these calls works?
你有没有注意到这些调用是如何工作的?
error_reporting(E_ERROR | E_WARNING | E_PARSE);
error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE);
error_reporting(E_ALL & ~E_NOTICE);
error_reporting(E_ALL);
So behind all above is game of bitwise operator and bits.
所以在所有这些后面都是位运算符和位元的游戏。
One usefull case of these is easy configurations like give below, so a single integer field can store thousands of combos for you.
其中一个有用的例子是如下所示的简单配置,因此一个整数字段可以为您存储数千个组合。
Most people have already read the docs but didn't reliase the real world use case of these bitwise operators.
大多数人已经阅读了文档,但是没有重新使用这些位操作符的真实世界用例。
Example That you 'll love
<?php
class Config {
// our constants must be 1,2,4,8,16,32,64 ....so on
const TYPE_CAT=1;
const TYPE_DOG=2;
const TYPE_LION=4;
const TYPE_RAT=8;
const TYPE_BIRD=16;
const TYPE_ALL=31;
private $config;
public function __construct($config){
$this->config=$config;
if($this->is(Config::TYPE_CAT)){
echo 'cat ';
}
if($this->is(Config::TYPE_DOG)){
echo 'dog ';
}
if($this->is(Config::TYPE_RAT)){
echo 'rat ';
}
if($this->is(Config::TYPE_LION)){
echo 'lion ';
}
if($this->is(Config::TYPE_BIRD)){
echo 'bird ';
}
echo "\n";
}
private function is($value){
return $this->config & $value;
}
}
new Config(Config::TYPE_ALL);
// cat dog rat lion bird
new Config(Config::TYPE_BIRD);
//bird
new Config(Config::TYPE_BIRD | Config::TYPE_DOG);
//dog bird
new Config(Config::TYPE_ALL & ~Config::TYPE_DOG & ~Config::TYPE_CAT);
//rat lion bird
#15
15
Null Coalesce Operator php
零合并算子php
The null coalescing operator (??) has been added to PHP7 for the common case of needing to use a ternary in conjunction with isset(). It returns its first operand if it exists and is not NULL otherwise it returns its second operand, such as following example:
对于需要与isset()一起使用三元组的常见情况,PHP7中添加了null coalescing运算符(?)。它返回它的第一个操作数,如果它存在并不是NULL,否则它将返回第二个操作数,例如下面的例子:
$username = $_GET['user'] ?? 'nobody';
// This is equivalent to:
$username = isset($_GET['user']) ? $_GET['user'] : 'nobody';
#16
14
Null Coalesce operator "??" (Added in PHP 7)
Not the catchiest name for an operator, but PHP 7 brings in the rather handy null coalesce so I thought I'd share an example.
对于操作符来说,这并不是最容易理解的名称,但是PHP 7引入了非常方便的空合并,所以我想我应该分享一个示例。
In PHP 5, we already have a ternary operator, which tests a value, and then returns the second element if that returns true and the third if it doesn't:
在PHP 5中,我们已经有一个三元运算符,它测试一个值,然后返回第二个元素(如果返回真),返回第三个元素(如果返回真):
echo $count ? $count : 10; // outputs 10
There is also a shorthand for that which allows you to skip the second element if it's the same as the first one: echo $count ?: 10; // also outputs 10
如果第二个元素与第一个元素相同,您也可以略过第二个元素:echo $count ?/ /输出10
In PHP 7 we additionally get the ?? operator which rather than indicating extreme confusion which is how I would usually use two question marks together instead allows us to chain together a string of values. Reading from left to right, the first value which exists and is not null is the value that will be returned.
在PHP 7中,我们还得到了??运算符,它不是表示极端的混乱,也就是我通常把两个问号放在一起使用,而是允许我们将一系列的值链接在一起。从左到右读取,存在的第一个值并不是null,是返回的值。
// $a is not set
$b = 16;
echo $a ?? 2; // outputs 2
echo $a ?? $b ?? 7; // outputs 16
This construct is useful for giving priority to one or more values coming perhaps from user input or existing configuration, and safely falling back on a given default if that configuration is missing. It's kind of a small feature but it's one that I know I'll be using as soon as my applications upgrade to PHP 7.
这个构造对于优先考虑来自用户输入或现有配置的一个或多个值是很有用的,如果缺少这个配置,则可以安全地返回到给定的默认值。这是一个小功能,但我知道我的应用程序升级到PHP 7时,我就会用到它。
#17
12
==
is used for check equality without considering variable data-type
==在不考虑变量数据类型的情况下使用。
===
is used for check equality for both the variable value* and **data-type
===用于检查变量值*和**数据类型的相等性
Example
$a = 5
一美元= 5
-
if ($a == 5)
- will evaluate to trueif ($a == 5) -将计算为true
-
if ($a == '5')
- will evaluate to true, because while comparing this both value php internally convert that string value into integer and then compare both valuesif ($a = '5') -将计算为true,因为在比较这个值时,两个值php都在内部将字符串值转换为integer,然后比较两个值
-
if ($a === 5)
- will evaluate to true如果($a === 5) -将计算为true
-
if ($a === '5')
- will evaluate to false, because value is 5, but this value 5 is not an integer.if ($a === '5') -将计算为false,因为值是5,但是这个值5不是整数。
#18
11
Here's the names of all the PHP operators (TOKEN).
这里是所有PHP操作符(令牌)的名称。
Reference http://php.net/manual/en/tokens.php
参考http://php.net/manual/en/tokens.php