引用——这个符号在PHP中意味着什么?

时间:2021-12-24 22:25:08

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

位运算符或引用


=& References

= &引用


&= Bitwise Operators

& =按位运算符


&& Logical Operators

逻辑运算符& &


% Arithmetic Operators

%算术运算符


!! Logical Operators

! !逻辑运算符


@ Error Control Operators

@误差控制操作符


?: Ternary Operator

:三元运算符


?? Null Coalesce Operator (since PHP 7)

? ?零合并运算符(从PHP 7开始)


: Alternative syntax for control structures, Ternary Operator

:控制结构的替代语法,三元运算符


:: Scope Resolution Operator

::范围解析操作符


\ Namespaces

\名称空间


-> Classes And Objects

- >类和对象


=> Arrays

= >数组


^ Bitwise Operators

^按位运算符


>> Bitwise Operators

> >按位运算符


<< Bitwise Operators

< <逐位运算符< p>


<<< Heredoc or Nowdoc

< < < Heredoc或Nowdoc


= Assignment Operators

=赋值操作符


== Comparison Operators

= =比较运算符


=== Comparison Operators

= = =比较运算符


!== Comparison Operators

! = =比较运算符


!= Comparison Operators

! =比较运算符


<> Comparison Operators

< >比较运算符


<=> Comparison Operators (since PHP 7.0)

<=>比较操作符(从PHP 7.0开始)


| Bitwise Operators

|按位运算符


|| Logical Operators

| |逻辑运算符


~ Bitwise Operators

~逐位运算符


+ Arithmetic Operators, Array Operators

+算术运算符,数组运算符


+= and -= Assignment Operators

+=和-=赋值操作符


++ and -- Incrementing/Decrementing Operators

++ -递增/递减运算符


.= Assignment Operators

=赋值操作符。


. String Operators

。字符串运算符


, Function Arguments

,函数参数

, Variable Declarations

,变量声明


$$ Variable Variables

变量$ $变量


` Execution Operator

“执行操作


<?= Short Open Tags

< ?=短开标签


[] Arrays (short syntax since PHP 5.4)

[]数组(PHP 5.4以来的简短语法)


<? Opening and Closing tags

< ?打开和关闭标签


... Argument unpacking (since PHP 5.6)

…参数解包(从PHP 5.6开始)


** Exponentiation (since PHP 5.6)

**指数(PHP 5.6)


# One-line shell-style comment

#一行shell风格的评论


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";
}

Live example

生活的例子

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  

_ Alias for gettext()

_别名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中添加的)中定义的。

Source

#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

花括号{ }

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 the key) 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 desired value.

    当使用关联数组时,键值将被放置在数组的索引中,以检索所需的值。

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

  1. if ($a == 5) - will evaluate to true

    if ($a == 5) -将计算为true

  2. 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 values

    if ($a = '5') -将计算为true,因为在比较这个值时,两个值php都在内部将字符串值转换为integer,然后比较两个值

  3. if ($a === 5) - will evaluate to true

    如果($a === 5) -将计算为true

  4. 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

引用——这个符号在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";
}

Live example

生活的例子

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  

_ Alias for gettext()

_别名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中添加的)中定义的。

Source

#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

花括号{ }

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 the key) 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 desired value.

    当使用关联数组时,键值将被放置在数组的索引中,以检索所需的值。

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

  1. if ($a == 5) - will evaluate to true

    if ($a == 5) -将计算为true

  2. 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 values

    if ($a = '5') -将计算为true,因为在比较这个值时,两个值php都在内部将字符串值转换为integer,然后比较两个值

  3. if ($a === 5) - will evaluate to true

    如果($a === 5) -将计算为true

  4. 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

引用——这个符号在PHP中意味着什么?