php函数参数中的&符号是啥意思

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php函数参数中的&符号是啥意思相关的知识,希望对你有一定的参考价值。

这是类中的一个函数
function base(& $get,& $post)
$this->time = time();
$this->ip =util::getip();
$this->get=& $get;
$this->post=& $post;
$this->init_db();
$this->init_cache();
$this->init_user();
$this->init_template();
$this->checkbanned();

其中的&符号是什么意思?

php中&符号即传的是变量的引用而不是拷贝,引用意味着用不同的名字访问同一个变量内容。这并不像 C 的指针,它们是符号表别名。注意在 PHP 中,变量名和变量内容是不一样的,因此同样的内容可以有不同的名字。最接近的比喻是 Unix 的文件名和文件本身 - 变量名是目录条目,而变量内容则是文件本身。引用可以被看作是 Unix 文件系统中的紧密连接。

PHP 的引用允许你用两个变量来指向同一个内容。意思是,当你这样做时:
<?php
$a =&$b
?>
这意味着 $a 和 $b 指向了同一个变量。
注: $a 和 $b 在这里是完全相同的,这并不是 $a 指向了 $b 或者相反,而是 $a 和 $b 指向了同一个地方。
同样的语法可以用在函数中,它返回引用,以及用在 new 运算符中(PHP 4.0.4 以及以后版本):
<?php
$bar =& new fooclass();
$foo =& find_var ($bar);
?>
注: 不用 & 运算符导致对象生成了一个拷贝。如果你在类中用 $this,它将作用于该类当前的实例。没有用 & 的赋值将拷贝这个实例(例如对象)并且 $this 将作用于这个拷贝上,这并不总是想要的结果。由于性能和内存消耗的问题,通常你只想工作在一个实例上面。
尽管你可以用 @ 运算符来关闭构造函数中的任何错误信息,例如用 @new,但用 &new 语句时这不起效果。这是 Zend 引擎的一个限制并且会导致一个解析错误。
引用做的第二件事是用引用传递变量。这是通过在函数内建立一个本地变量并且该变量在呼叫范围内引用了同一个内容来实现的。例如:
<?php
function foo (&$var)
$var++;

$a=5;
foo ($a);
?>
将使 $a 变成 6。这是因为在 foo 函数中变量 $var 指向了和 $a 指向的同一个内容。更多详细解释见引用传递。
引用做的第三件事是引用返回。
引用不是什么
如前所述,引用不是指针。这意味着下面的结构不会产生你预期的效果:
<?php
function foo (&$var)
$var =& $GLOBALS["baz"];

foo($bar);
?>
这将使 foo 函数中的 $var 变量在函数调用时和 $bar 绑定在一起,但接着又被重新绑定到了 $GLOBALS["baz"] 上面。不可能通过引用机制将 $bar 在函数调用范围内绑定到别的变量上面,因为在函数 foo 中并没有变量 $bar(它被表示为 $var,但是 $var 只有变量内容而没有调用符号表中的名字到值的绑定)。
引用传递
你可以将一个变量通过引用传递给函数,这样该函数就可以修改其参数的值。语法如下:
<?php
function foo (&$var)
$var++;

$a=5;
foo ($a);
// $a is 6 here
?>
注意在函数调用时没有引用符号 - 只有函数定义中有。光是函数定义就足够使参数通过引用来正确传递了。
以下内容可以通过引用传递:
变量,例如 foo($a)
New 语句,例如 foo(new foobar())
从函数中返回的引用,例如:
<?php
function &bar()
$a = 5;
return $a;

foo(bar());
?>
详细解释见引用返回。
任何其它表达式都不能通过引用传递,结果未定义。例如下面引用传递的例子是无效的:
<?php
function bar() // Note the missing &
$a = 5;
return $a;

foo(bar());
foo($a = 5) // 表达式,不是变量
foo(5) // 常量,不是变量
?>
这些条件是 PHP 4.0.4 以及以后版本有的。
引用返回
引用返回用在当你想用函数找到引用应该被绑定在哪一个变量上面时。当返回引用时,使用此语法:
<?php
function &find_var ($param)
/* ...code... */
return $found_var;

$foo =& find_var ($bar);
$foo->x = 2;
?>
本例中 find_var 函数所返回的对象的属性将被设定(译者:指的是 $foo->x = 2; 语句),而不是拷贝,就和没有用引用语法一样。
注: 和参数传递不同,这里必须在两个地方都用 & 符号 - 来指出返回的是一个引用,而不是通常的一个拷贝,同样也指出 $foo 是作为引用的绑定,而不是通常的赋值。
取消引用
当你 unset 一个引用,只是断开了变量名和变量内容之间的绑定。这并不意味着变量内容被销毁了。例如:
<?php
$a = 1;
$b =& $a;
unset ($a);
?>
不会 unset $b,只是 $a。
再拿这个和 Unix 的 unlink 调用来类比一下可能有助于理解。
引用定位
许多 PHP 的语法结构是通过引用机制实现的,所以上述有关引用绑定的一切也都适用于这些结构。一些结构,例如引用传递和返回,已经在上面提到了。其它使用引用的结构有:
global 引用
当用 global $var 声明一个变量时实际上建立了一个到全局变量的引用。也就是说和这样做是相同的:
<?php
$var =& $GLOBALS["var"];
?>
这意味着,例如,unset $var 不会 unset 全局变量。
$this
在一个对象的方法中,$this 永远是调用它的对象的引用。
参考技术A &有两种功能。 1. 按位与 2.引用。 这里使用的是引用的功能。

这里的base函数把 $post和$get的引用传递给了 当前对象的post 和get属性中。

这样 在以后改变当前对象的 post和get 属性的值以后 之前定义的$post和$get 变量的值也跟着改变。 这就是引用。
下面是我写的测试代码,运行一下就明白了。
<?php
$get = 1;
$base($get);
echo $get ;
//输出结果会是 2 而不是 1
function base(& $get)
$get++;

?>本回答被提问者和网友采纳
参考技术B 相当于指针

参考——这个符号在 PHP 中是啥意思?

【中文标题】参考——这个符号在 PHP 中是啥意思?【英文标题】:Reference — What does this symbol mean in PHP?参考——这个符号在 PHP 中是什么意思? 【发布时间】:2011-04-13 19:43:37 【问题描述】:

这是什么?

这是一个关于 PHP 语法的问题的集合。这也是一个社区 Wiki,因此邀请所有人参与维护此列表。

这是为什么?

过去很难找到有关运算符和其他语法标记的问题。¹ 主要思想是在 Stack Overflow 上提供现有问题的链接,以便我们更轻松地引用它们,而不是复制 PHP 手册中的内容。

注意:自 2013 年 1 月起,Stack Overflow does support special characters。只需用引号将搜索词括起来,例如[php] "==" vs "==="

我应该在这里做什么?

如果您因为提出这样的问题而被某人指向这里,请在下面找到特定的语法。 PHP manual 的链接页面以及链接的问题可能会回答您的问题。如果是这样,我们鼓励您投票赞成答案。此列表不能替代其他人提供的帮助。

名单

如果您的特定令牌未在下面列出,您可能会在 List of Parser Tokens 中找到它。


&amp;Bitwise Operators 或References

What does it mean to start a PHP function with an ampersand? Understanding PHP & (ampersand, bitwise and) operator PHP "&" operator Difference between & and && in PHP What does "&" mean here in PHP? What does "&" mean in this case? What does the "&" sign mean in PHP? What does this signature mean (&) in PHP? How does the "&" operator work in a PHP function? What does & in &2 mean in PHP? When should I use a bitwise operator? Is there ever a need to use ampersand in front of an object? (&$)

=&amp;References

Reference assignment operator in PHP, =& What do the "=&" and "&=" operators in PHP mean? What do the '&=' and '=&' operators do? What does =& mean in PHP?

&amp;=Bitwise Operators

What do the "=&" and "&=" operators in PHP mean? What do the '&=' and '=&' operators do?

&amp;&amp;Logical Operators

'AND' vs '&&' as operator Difference between & and && in PHP Is there any difference between "and" and "&&" operators in PHP? PHP - and / or keywords

%Arithmetic Operators

What does the percent sign mean in PHP? What is the PHP operator % and how do I use it in real-world examples?

!!Logical Operators

Double not (!!) operator in PHP

@Error Control Operators

What is the use of the @ symbol in PHP? 'At' symbol before variable name in PHP: @$_POST PHP functions and @functions Should I use @ in my PHP code? What does @ mean in PHP?

?:Ternary Operator

What are the PHP operators "?" and ":" called and what do they do? ?: operator (the 'Elvis operator') in PHP Where can I read about conditionals done with "?" and ":" (colon)? Using PHP 5.3 ?: operator

??Null Coalesce Operator(PHP 7 起)

C#'s null coalescing operator (??) in PHP

?string ?int ?array ?bool ?floatNullable return type declaration(PHP 7.1 起)

Nullable return type declaration php method argument type hinting with question mark (?type)

:Alternative syntax for control structures、Ternary Operator、Return Type Declaration

What is ":" in PHP? What does ":" mean in PHP? Colon after method declaration?

::Scope Resolution Operator

What do two colons mean in PHP? What's the meaning of the PHP token name T_PAAMAYIM_NEKUDOTAYIM? What's the difference between :: (double colon) and -> (arrow) in PHP? What exactly are late static bindings in PHP? static::staticFunctionName() Unexpected T_PAAMAYIM_NEKUDOTAYIM, expecting T_NS_Separator

\Namespaces

Backslash in PHP -- what does it mean? What does a \ (backslash) do in PHP (5.3+)?

-&gt;Classes And Objects

What is the "->" PHP operator called? Where do we use the object operator "->" in PHP? What's the difference between :: (double colon) and -> (arrow) in PHP? What does the PHP syntax $var1->$var2 mean? What does "->" mean/refer to in PHP?

=&gt;Arrays

What does "=>" mean in PHP? Use of => in PHP What does $k => $v in foreach($ex as $k=>$v) mean?

^Bitwise Operators

How does the bitwise operator XOR ('^') work? What does ^ mean in PHP?

&gt;&gt;Bitwise Operators

What does >> mean in PHP?

&lt;&lt;Bitwise Operators

Strange print behaviour in PHP?

&lt;&lt;&lt;Heredoc or Nowdoc

What does <<<END mean in PHP? PHP expression <<<EOB In PHP, what does "<<<" represent? Using <<<CON in PHP What's this kind of syntax in PHP?

=Assignment Operators

The 3 different equals

==Comparison Operators

How do the PHP equality (== double equals) and identity (=== triple equals) comparison operators differ? PHP != and == operators The 3 different equals Type-juggling and (strict) greater/lesser-than comparisons in PHP

===Comparison Operators

What does "===" mean? How do the PHP equality (== double equals) and identity (=== triple equals) comparison operators differ? The 3 different equals Type-juggling and (strict) greater/lesser-than comparisons in PHP

!==Comparison Operators

What does !== comparison operator in PHP mean? Is there a difference between !== and != in PHP?

!=Comparison Operators

PHP != and == operators Is there a difference between !== and != in PHP? comparing, !== versus != What is the difference between <> and !=

&lt;&gt;Comparison Operators

PHP operator <> PHP's <> operator What is the difference between <> and != Type-juggling and (strict) greater/lesser-than comparisons in PHP

&lt;=&gt;Comparison Operators(PHP 7.0 起)

Spaceship (three way comparison) operator

|Bitwise Operators

What is the difference between the | and || operators? What Does Using A Single Pipe '|' In A Function Argument Do?

||Logical Operators

What is the difference between the | and || operators? PHP - and / or keywords What exactly does || mean? The behaviour of the or operator in PHP

~Bitwise Operators

What does this ~ operator mean here?

+Arithmetic Operators,Array Operators

+ operator for array in PHP?

+=-= Assignment Operators

What is += used for? What does `$page -= 1` in my PHP document mean?

++-- Incrementing/Decrementing Operators

Understanding Incrementing Answer below

.=Assignment Operators

What is the difference between .= and += in PHP? To understand a line of PHP

.String Operators

Difference between period and comma when concatenating with echo versus return? What does a . (dot) do in PHP?

,Function Arguments

Difference between period and comma when concatenating with echo versus return?

,Variable Declarations

What do commas mean in a variable declaration?

$$Variable Variables

What does $$ (dollar dollar or double dollar) mean in PHP? what is "$$" in PHP $function() and $$variable

`Execution Operator

What are the backticks `` called?

&lt;?=Short Open Tags

What does this symbol mean in PHP <?= What does '<?=' mean in PHP? What does <?= mean?

[]Arrays(PHP 5.4 以来的短语法)

PHP arrays... What is/are the meaning(s) of an empty bracket? What is the meaning of [] Php array_push() vs myArray[] What does [] mean when reading from a PHP array? Shorthand for arrays: literal $var = [] empty array

&lt;?Opening and Closing tags

Are PHP short tags acceptable to use?

...Argument unpacking(PHP 5.6 起)


**Exponentiation(PHP 5.6 起)


#One-line shell-style comment

Can I use hashes for comments in PHP?

?-&gt;NullSafe Operator Calls(PHP 8.0 起)

What does this symbol mean "?->" in php, within an object or null value

【问题讨论】:

我知道这不是严格意义上的 PHP,但是包含一个指向 phpdoc.org 的链接,用于 phpDocumentor 注释语法,这是常用的,也无法搜索到 /** 我可以建议方括号和大括号吗? 我也经常遇到这个问题(无法搜索特殊字符),这就是为什么我制作了SymbolHound,一个不会忽略特殊字符的搜索引擎。我还在StackApps上发布了它。 嗯,从标题 为什么会这样?,我猜这是因为“主要想法是在 Stack Overflow 上提供现有问题的链接,这样更容易我们参考他们”。 今天(2015 年 11 月 20 日)***.com/questions/33833259/what-is-rscat-in-php 问了一个问题 "What is $rsCat in php" )。奇怪的是,没有关于$ 变量的参考,而只有关于$$ 变量的参考。我认为应该在某个地方进行修改。 【参考方案1】:

Incrementing / Decrementing Operators

++自增运算符

--递减运算符

Example    Name              Effect
---------------------------------------------------------------------
++$a       Pre-increment     Increments $a by one, then returns $a.
$a++       Post-increment    Returns $a, then increments $a by one.
--$a       Pre-decrement     Decrements $a by one, then returns $a.
$a--       Post-decrement    Returns $a, then decrements $a by one.

这些可以放在变量之前或之后。

如果放在变量之前,则对变量进行递增/递减操作,然后返回结果。如果放在变量之后,则返回变量,然后进行递增/递减操作。

例如:

$apples = 10;
for ($i = 0; $i < 10; ++$i) 
    echo 'I have ' . $apples-- . " apples. I just ate one.\n";

Live example

在上面的例子中使用++$i,因为它更快。 $i++ 会得到相同的结果。

预增量更快一点,因为它确实增加了变量,然后“返回”结果。后增量创建一个特殊变量,将第一个变量的值复制到那里,只有在第一个变量被使用后,才用第二个变量替换它的值。

但是,您必须使用$apples--,因为首先,您要显示当前的苹果数量,然后您要从中减去一个。

您还可以在 PHP 中增加字母:

$i = "a";
while ($i < "c") 
    echo $i++;

一旦到达z,接下来就是aa,依此类推。

请注意,字符变量可以递增但不能递减,即使如此,也仅支持纯 ASCII 字符(a-z 和 A-Z)。


堆栈溢出帖子:

Understanding Incrementing

【讨论】:

为了大家的利益,请删除关于预增量无限快的粗体信息。这绝对是过早优化最糟糕的例子,如果他们刚刚开始学习 PHP,这种信息不应该出现在人们的脑海中。 @Lotus - 我认为这是一个有趣的事实。如果您是 PHP 或 C++ 等的初学者,那么 ++i 和 i++ 的差异足以以不同的速度工作似乎很古怪。我觉得它很吸引人。 @Peter Ajtai 是的,这很有趣,但是从您构建帖子的方式来看,您使它看起来像是 PHP 的主要事实之一,这对于使用该语言绝对至关重要。【参考方案2】:
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 pairsin the same order and of the same types
x !== y Non-identity true if x is not identical to y
x &lt;=&gt; y Spaceship 0 if x is equal to y, greater than 0 if x > y, less than 0 if x < y
++x Pre-increment Increments x by one, then returns x
x++ Post-increment Returns x, then increments x by one
--x Pre-decrement Decrements x by one, then returns x
x-- Post-decrement Returns x, then decrements x by one
x and y And true if both x and y are true. If x=6, y=3 then(x &lt; 10 and y &gt; 1) returns true
x &amp;&amp; y And true if both x and y are true. If x=6, y=3 then(x &lt; 10 &amp;&amp; y &gt; 1) returns true
x or y Or true if any of x or y are true. If x=6, y=3 then(x &lt; 10 or y &gt; 10) returns true
x || y Or true if any of x or y are true. If x=6, y=3 then(x &lt; 3 || y &gt; 1) returns true
a . b Concatenation Concatenate two strings: "Hi" . "Ha"

【讨论】:

【参考方案3】:

位运算符

什么是一点?位是 1 或 0 的表示。基本上是 OFF(0) 和 ON(1)

什么是字节?一个字节由 8 位组成,一个字节的最高值为 255,这意味着每个位都已设置。我们将看看为什么一个字节的最大值是 255。

-------------------------------------------
|      1 Byte ( 8 bits )                  |
-------------------------------------------
|Place Value | 128| 64| 32| 16| 8| 4| 2| 1|     
-------------------------------------------

这个表示1字节

1 + 2 + 4 + 8 + 16 + 32 + 64 + 128 = 255(1 字节)

几个例子便于理解

“与”运算符:&amp;

$a =  9;
$b = 10;
echo $a & $b;

这将输出数字 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|
------------------------------------------- 

所以你可以从表中看到它们共同共享的唯一位是 8 位。

第二个例子

$a =  36;
$b = 103;
echo $a & $b; // This would output the number 36.
$a = 00100100
$b = 01100111

这两个共享位是 32 和 4,当它们相加时返回 36。

“或”运算符:|

$a =  9;
$b = 10;
echo $a | $b;

这会输出数字 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|
-------------------------------------------

您会注意到我们在 8、2 和 1 列中设置了 3 位。将它们相加:8+2+1=11。

【讨论】:

如果 $a 的值大于 255 怎么办? @AycanYaşıt 大多数操作系统都使用 32 位和 64 位系统,这意味着限制远远超过 255(8 位)。 @AycanYaşıt 实际上,这里用一个字节长度表示甚至不正确,因为在现代 64 位平台上,即使最小的整数在内存中仍然是 64 位(8 字节)。 为什么and &amp;0 0 0 0 1 0 0 0or |0 0 0 0 1 0 1 1【参考方案4】:

_ Alias for gettext()

_() 中的下划线字符“_”是gettext() 函数的别名。

【讨论】:

知道什么是双下划线功能吗? __()。它用于 WordPress。我在任何地方都找不到它的定义。使用示例:core.trac.wordpress.org/browser/tags/5.4/src/wp-admin/includes/…。看来要使用 WP image_size_names_choose() 过滤器,必须使用 __() 将“短名称”添加到要添加的 named_sizes 数组中。 @SherylHohman 看起来是翻译功能。 developer.wordpress.org/reference/functions/__ 非常感谢!我羡慕你出色的 Google 技能(和丰富的编码知识):-)【参考方案5】:

魔法常数:虽然这些不仅是符号,而且是这个令牌家族的重要组成部分。有八个神奇的常数会根据它们的使用位置而变化。

__LINE__:文件的当前行号。

__FILE__:文件的完整路径和文件名。如果在包含中使用,则返回包含文件的名称。自 PHP 4.0.2 起,__FILE__ 始终包含一个绝对路径,符号链接已解析,而在旧版本中,它在某些情况下包含相对路径。

__DIR__:文件所在目录。如果在包含中使用,则返回包含文件的目录。这相当于dirname(__FILE__)。此目录名称没有尾部斜杠,除非它是根目录。 (在 PHP 5.3.0 中添加。)

__FUNCTION__:函数名。 (在 PHP 4.3.0 中添加)从 PHP 5 开始,此常量返回声明时的函数名称(区分大小写)。在 PHP 4 中,它的值总是小写的。

__CLASS__:类名。 (在 PHP 4.3.0 中添加)从 PHP 5 开始,该常量返回声明的类名(区分大小写)。在 PHP 4 中,它的值总是小写的。类名包括它被声明的命名空间(例如Foo\Bar)。请注意,从 PHP 5.4 开始,__CLASS__ 也适用于特征。在 trait 方法中使用时,__CLASS__ 是使用 trait 的类的名称。

__TRAIT__:特征名称。 (在 PHP 5.4.0 中添加)从 PHP 5.4 开始,此常量返回声明时的 trait(区分大小写)。特征名称包括声明它的命名空间(例如Foo\Bar)。

__METHOD__:类方法名。 (在 PHP 5.0.0 中添加)方法名称在声明时返回(区分大小写)。

__NAMESPACE__:当前命名空间的名称(区分大小写)。此常量在编译时定义(PHP 5.3.0 中添加)。

Source

【讨论】:

【参考方案6】:

类型运算符

instanceof用于判断PHP变量是否为某个类的实例化对象。

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

上面的例子会输出:

bool(true)
bool(false)

原因:以上示例$amclass 的对象,因此仅使用mclass 数据而不是sclass 的实例

继承示例

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

上面的例子会输出:

bool(true)
bool(true)

克隆示例

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

上面的例子会输出:

bool(true)
bool(true)

【讨论】:

以上内容也适用于“接口”。这对于检查特定接口是否可用很有用。【参考方案7】:

&lt;=&gt;宇宙飞船操作员

在 PHP 7 中添加

spaceship operator&lt;=&gt; 是 PHP 7 中最新添加的比较运算符。它是一个非关联二元运算符,与相等运算符具有相同的优先级(==!====!==)。此运算符允许在左侧和右侧操作数之间进行更简单的三向比较。

该运算符产生一个整数表达式:

0 当两个操作数相等时 当左侧操作数小于右侧操作数时小于0 当左侧操作数大于右侧操作数时大于0

例如

1 <=> 1; // 0
1 <=> 2; // -1
2 <=> 1; // 1

使用此运算符的一个很好的实际应用是比较类型的回调,该回调预计基于两个值之间的三向比较返回零、负或正整数。传递给usort 的比较函数就是这样一个例子。

在 PHP 7 之前你会写...

$arr = [4,2,1,3];

usort($arr, function ($a, $b) 
    if ($a < $b) 
        return -1;
     elseif ($a > $b) 
        return 1;
     else 
        return 0;
    
);

从 PHP 7 开始你可以写...

$arr = [4,2,1,3];

usort($arr, function ($a, $b) 
    return $a <=> $b;
    // return $b <=> $a; // for reversing order
);

【讨论】:

不确定$a &lt;=&gt; $b$a - $b 有何不同 @AcidShout $a - $b 适用于数字,但不适用于字符串、对象或数组。 @mcrumley 不,比这更糟。一般来说$a - $b 甚至不适用于数字;它仅适用于 整数。它不适用于非整数,因为usort 将比较器函数的返回值转换为int,这意味着 0.5 被转换为 0,这意味着两个相差小于 1 的数字,例如 4和 4.6,可能(取决于哪个作为第一个参数传递给您的比较器函数)错误地比较为相等。 @MarkAmery 迁移指南不是操作员的记录行为。为此,您想查看 php.net/language.operators.comparison 手册的语言运算符部分,这背后的实际行为依赖于 API 的各种比较函数,例如当您对字符串执行 strcmp 时,您无法保证实际在每种情况下都返回值。当然,它几乎总是 1、0 或 -1,但对于无法像包装 libc 的 strcmp 那样保证它的情况,您提供与底层规范相同的定义行为以确保安全 @MarkAmery 这里的重点是不允许人们依赖未定义的行为。对于某人得到的值不完全是 1、0 或 -1 的一种情况,您会得到某人提交错误报告,认为该语言存在问题。这就是为什么我们记录了我们所能保证的只是该值将小于大于等于 0,并且不一定1、0 和 -1。【参考方案8】:

宇宙飞船操作员&lt;=&gt;(在 PHP 7 中添加)

Examples for &lt;=&gt; Spaceship operator (PHP 7, Source: PHP Manual):

用于变量三向比较的整数、浮点数、字符串、数组和对象。

// Integers
echo 10 <=> 10; // 0
echo 10 <=> 20; // -1
echo 20 <=> 10; // 1

// Floats
echo 1.5 <=> 1.5; // 0
echo 1.5 <=> 2.5; // -1
echo 2.5 <=> 1.5; // 1

// Strings
echo "a" <=> "a"; // 0
echo "a" <=> "b"; // -1
echo "b" <=> "a"; // 1
// Comparison is case-sensitive
echo "B" <=> "a"; // -1

echo "a" <=> "aa"; // -1
echo "zz" <=> "aa"; // 1

// Arrays
echo [] <=> []; // 0
echo [1, 2, 3] <=> [1, 2, 3]; // 0
echo [1, 2, 3] <=> []; // 1
echo [1, 2, 3] <=> [1, 2, 1]; // 1
echo [1, 2, 3] <=> [1, 2, 4]; // -1

// Objects
$a = (object) ["a" => "b"]; 
$b = (object) ["a" => "b"]; 
echo $a <=> $b; // 0

$a = (object) ["a" => "b"]; 
$b = (object) ["a" => "c"]; 
echo $a <=> $b; // -1

$a = (object) ["a" => "c"]; 
$b = (object) ["a" => "b"]; 
echo $a <=> $b; // 1

// only values are compared
$a = (object) ["a" => "b"]; 
$b = (object) ["b" => "b"]; 
echo $a <=> $b; // 1

【讨论】:

【参考方案9】:

花括号

Blocks - curly braces/no curly braces? Curly braces in string in PHP PHP curly braces in array notation

关于上一篇文章的一些话

$x[4] = 'd'; // it works
$x4 = 'd'; // it works

$echo $x[4]; // it works
$echo $x4; // 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 "$x4"; // does not work

echo "$x[4]"; // it works
echo "$x4"; // it works

【讨论】:

属性名称的 '' 表示法。 echo $movies->movie->'great-lines'->line;可用于访问 SimpleXMLElement 中的数据。也许它也可以用于其他情况。 php.net/manual/en/simplexml.examples-basic.php【参考方案10】:

PHP 字符串: PHP 字符串可以通过四种方式指定,而不仅仅是两种方式:

1) 单引号字符串:

$string = 'This is my string'; // print This is my string

2) 双引号字符串:

$str = 'string';

$string = "This is my $str"; // print This is my string

3)Heredoc:

$string = <<<EOD
This is my string
EOD; // print This is my string

4) Nowdoc(自 PHP 5.3.0 起):

$string = <<<'END_OF_STRING'
    This is my string 
END_OF_STRING; // print This is my string

【讨论】:

@Rizier123 : “Heredocs 不能用于初始化类属性。自 PHP 5.3 起,此限制仅对包含变量的 heredocs 有效。”是什么意思?【参考方案11】:

operators in PHP概述:


Logical Operators:

$a && $b : 如果 $a 和 $b 都为 TRUE,则为 TRUE。 $a || $b : 如果 $a 或 $b 为 TRUE,则为 TRUE。 $a xor $b : 如果 $a 或 $b 为 TRUE,但不能同时为 TRUE,则为 TRUE。 ! $a : 如果 $a 不为 TRUE,则为 TRUE。 $a 和 $b : 如果 $a 和 $b 都为 TRUE,则为 TRUE。 $a 或 $b : 如果 $a 或 $b 为 TRUE,则为 TRUE。

Comparison operators:

$a == $b : 如果在类型杂耍之后 $a 等于 $b,则为 TRUE。 $a === $b : 如果 $a 等于 $b 并且它们属于同一类型,则为 TRUE。 $a != $b : 如果在类型杂耍之后 $a 不等于 $b,则为 TRUE。 $a $b : 如果在类型杂耍之后 $a 不等于 $b,则为 TRUE。 $a !== $b : 如果 $a 不等于 $b,或者它们不是同一类型,则为 TRUE。 $a :如果 $a 严格小于 $b,则为 TRUE。 $a > $b :如果 $a 严格大于 $b,则为 TRUE。 $a :如果 $a 小于或等于 $b,则为 TRUE。 $a >= $b :如果 $a 大于或等于 $b,则为 TRUE。 $a $b :当 $a 分别小于、等于或大于 $b 时,小于、等于或大于零的整数。自 PHP 7 起可用。 $a ? $b : $c : if $a return $b else return $c (ternary operator) $a ?? $c :和 $a 一样吗? $a : $c (null coalescing operator - 需要 PHP>=7)

Arithmetic Operators:

-$a :$a 的对面。 $a + $b :$a 和 $b 的总和。 $a - $b : $a 和 $b 的区别。 $a * $b :$a 和 $b 的乘积。 $a / $b : $a 和 $b 的商。 $a % $b :$a 的余数除以 $b。 $a ** $b : 将 $a 提高到 $b' 次方的结果(在 PHP 5.6 中引入)

Incrementing/Decrementing Operators:

++$a :将 $a 加一,然后返回 $a。 $a++ :返回 $a,然后将 $a 加一。 --$a :将 $a 减一,然后返回 $a。 $a-- :返回 $a,然后将 $a 减一。

Bitwise Operators:

$a & $b :在 $a 和 $b 中设置的位都已设置。 $a | $b :在 $a 或 $b 中设置的位被设置。 $a ^ $b :在 $a 或 $b 中设置但未同时设置的位。 ~ $a : $a 中设置的位未设置,反之亦然。 $a : 将$a $b 的位向左移动(每一步的意思是“乘以2”) $a >> $b : 将 $a $b 的位向右移动(每一步的意思是“除以二”)

String Operators:

$a 。 $b :$a 和 $b 的串联。

Array Operators:

$a + $b : $a 和 $b 的并集。 $a == $b :如果 $a 和 $b 具有相同的键/值对,则为 TRUE。 $a === $b:如果 $a 和 $b 具有相同的键/值对且顺序相同且类型相同,则为 TRUE。 $a != $b :如果 $a 不等于 $b,则为 TRUE。 $a $b :如果 $a 不等于 $b,则为 TRUE。 $a !== $b :如果 $a 与 $b 不同,则为 TRUE。

Assignment Operators:

$a = $b : $b 的值赋给 $a $a += $b :与 $a = $a + $b 相同 $a -= $b :与 $a = $a - $b 相同 *$a = $b :与 $a = $a * $b 相同 $a /= $b :与 $a = $a / $b 相同 $a %= $b :与 $a = $a % $b 相同 **$a = $b : 与 $a = $a ** $b 相同 $a .= $b :与 $a = $a 相同。 $b $a &= $b :与 $a = $a & $b 相同 $a |= $b :与 $a = $a | 相同$b $a ^= $b :与 $a = $a ^ $b 相同 $a :与 $a = $a 相同 $a >>= $b :与 $a = $a >> $b 相同 $a ??= $b :如果 $a 为 null 或未定义,则 $b 的值分配给 $a(null coalescing assignment operator - 需要 PHP>=7.4)

注意

and 运算符和or 运算符的优先级低于赋值运算符=

这意味着$a = true and false; 等价于($a = true) and false

在大多数情况下,您可能希望使用 &amp;&amp;||,它们的行为方式与 C、Java 或 JavaScript 等语言的行为方式相同。

【讨论】:

$a ?? $c有一个错误,它说和$a ? $a : $c一样,但是三元运算符检查值是否为真,另一方面,空值合并检查空值,所以,如果 $a 为 0,你将得到 0(因为 0 不为空),例如,如果你有:$a=0; $c=5; 然后$a?$a:$c 返回 5,$a??$c 返回 0。【参考方案12】:

问题:

=&gt; 是什么意思?


回答:

=&gt; 是我们人类决定用来分隔关联数组中的"Key" =&gt; "Value" 对的符号。

详细说明:

要理解这一点,我们必须知道什么是关联数组。当常规程序员想到数组(在 PHP 中)时,首先会出现类似于以下内容的内容:

$myArray1 = array(2016, "hello", 33);//option 1

$myArray2 = [2016, "hello", 33];//option 2

$myArray3 = [];//option 3
$myArray3[] = 2016; 
$myArray3[] = "hello"; 
$myArray3[] = 33;

如果我们想在后面的代码中调用数组,我们可以这样做:

echo $myArray1[1];// output: hello
echo $myArray2[1];// output: hello
echo $myArray3[1];// output: hello

到目前为止一切顺利。但是,作为人类,我们可能很难记住数组的索引[0]year 2016 的值,数组的索引[1]问候 strong>,数组的索引[2] 是一个简单的整数值。我们将有的替代方法是使用所谓的关联数组。关联数组与 顺序数组 有一些不同之处 (这就是以前的情况,因为它们以预定的顺序递增使用的索引,为每个后续值递增 1)。

区别(顺序数组和关联数组之间):

在声明关联数组时,您不仅要包含要放入数组的value,还要放入要使用的索引值(称为key)在代码的后面部分调用数组时。在其声明期间使用以下语法:"key" =&gt; "value"

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

例如:

$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;

现在,要接收与以前相同的输出,将在数组索引中使用 key 值:

echo $myArray1["Greetings"];// output: hello
echo $myArray2["Greetings"];// output: hello
echo $myArray3["Greetings"];// output: hello

最后一点:

所以从上面的例子中,很容易看出=&gt; 符号用于表示数组中每个keyvalue 对之间的关​​联数组的关系DURING 数组中值的初始化。

【讨论】:

【参考方案13】:

空合并运算符 (??)

这个运算符已在 PHP 7.0 中添加,用于需要将三元运算符与 isset() 结合使用的常见情况。如果存在且不是NULL,则返回其第一个操作数;否则返回第二个操作数。

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

【讨论】:

【参考方案14】:

问题

“&”在 PHP 中是什么意思?

PHP“&”运算符

一旦我们习惯了它会让生活更轻松..(仔细查看下面的示例)

& 通常检查在 $a 和 $b 中设置的位是否已设置。

你有没有注意到这些调用是如何工作的?

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

所以在这一切的背后是位运算符和位的游戏。

其中一个有用的例子是像下面给出的简单配置,因此单个整数字段可以为您存储数千个组合。

大多数人已经阅读过文档,但并未了解这些位运算符的实际用例。

你会喜欢的例子

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

== 用于检查相等性不考虑变量数据类型

=== 用于检查 both 变量 valuedata-type

的相等性

示例

$a = 5

    if ($a == 5) - 评估结果为真

    if ($a == '5') - 将评估为真,因为在比较这两个值时,PHP 在内部将该字符串值转换为整数,然后比较两个值

    if ($a === 5) - 评估结果为真

    if ($a === '5') - 将评估为 false,因为值为 5,但此值 5 不是整数。

【讨论】:

【参考方案16】:

空合并运算符“??” (在 PHP 7 中添加)

对于运算符来说,这不是最吸引人的名称,但 PHP 7 带来了相当方便的 null 合并,所以我想我会分享一个示例。

在 PHP 5 中,我们已经有了一个三元运算符,它测试一个值,如果返回 true,则返回第二个元素,否则返回第三个元素:

echo $count ? $count : 10; // outputs 10

如果第二个元素与第一个元素相同,还有一个简写方式可以让您跳过第二个元素:echo $count ?: 10; // 也输出 10

在 PHP 7 中,我们还获得了 ??运算符,而不是表示极度混乱,这就是我通常将两个问号一起使用的方式,而是允许我们将一串值链接在一起。从左到右读取,第一个存在且不为空的值就是要返回的值。

// $a is not set
$b = 16;

echo $a ?? 2; // outputs 2
echo $a ?? $b ?? 7; // outputs 16

此构造对于优先考虑可能来自用户输入或现有配置的一个或多个值很有用,并在缺少该配置时安全地回退到给定的默认值。这是一个小功能,但我知道一旦我的应用程序升级到 PHP 7,我就会使用它。

【讨论】:

【参考方案17】:

可空返回类型声明

PHP 7 增加了对返回类型声明的支持。与参数类型声明类似,返回类型声明指定将从函数返回的值的类型。可用于返回类型声明的类型与可用于参数类型声明的类型相同。

严格类型也会对​​返回类型声明产生影响。在默认的弱模式下,如果返回的值还不是正确的类型,它们将被强制转换为正确的类型。在强模式下,返回值必须是正确的类型,否则会抛出TypeError。

从 PHP 7.1.0 开始,可以通过在类型名称前加上问号 (?) 将返回值标记为可为空。这表示该函数返回指定的类型或 NULL。

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

?>

Source

【讨论】:

【参考方案18】:

三个 DOTS 作为 Splat 运算符 (...)(自 PHP 5.6 起)

PHP 有一个运算符“...”(三个点),称为 Splat 运算符。它用于在函数中传递任意数量的参数,这种类型的函数称为可变参数函数。让我们举个例子来使用“...”(三个点)。

示例 1:

<?php
function calculateNumbers(...$params)
    $total = 0;
    foreach($params as $v)
        $total = $total + $v;
    
    return $total;


echo calculateNumbers(10, 20, 30, 40, 50);

//Output 150
?>

calculateNumbers() 函数的每个参数在使用“...”时作为一个数组传递给 $params。

有许多不同的方式来使用“...”操作符。下面是一些例子:

示例 2:

<?php
function calculateNumbers($no1, $no2, $no3, $no4, $no5)
    $total = $no1 + $no2 + $no3 + $no4 + $no5;
    return $total;


$numbers = array(10, 20, 30, 40, 50);
echo calculateNumbers(...$numbers);

//Output 150
?>

示例 3:

<?php
function calculateNumbers(...$params)
    $total = 0;
    foreach($params as $v)
        $total = $total + $v;
    
    return $total;

$no1 = 70;
$numbers = array(10, 20, 30, 40, 50);
echo calculateNumbers($no1, ...$numbers);

//Output 220
?>

示例 4:

<?php
function calculateNumbers(...$params)
    $total = 0;
    foreach($params as $v)
        $total = $total + $v;
    
    return $total;


$numbers1 = array(10, 20, 30, 40, 50);
$numbers2 = array(100, 200, 300, 400, 500);
echo calculateNumbers(...$numbers1, ...$numbers2);

//Output 1650

?>

【讨论】:

[TODO] 在 PHP8 中包含新功能以使用带有“命名参数”的 splat 运算符参考:***.com/a/64997399/2943403【参考方案19】:

?-> NullSafe 运算符

在 PHP 8.0 中添加

它是NullSafe Operator,它返回null,以防您尝试调用函数或从null 获取值。 Nullsafe 运算符可以被链接起来,并且可以在方法和属性上使用。

$objDrive = null;
$drive = $objDrive?->func?->getDriver()?->value; //return null
$drive = $objDrive->func->getDriver()->value; // Error: Trying to get property 'func' of non-object

Nullsafe 运算符不适用于数组键:

$drive['admin']?->getDriver()?->value //Warning: Trying to access array offset on value of type null

$drive = [];
$drive['admin']?->getAddress()?->value //Warning: Undefined array key "admin"

【讨论】:

添加这个感觉还为时过早,没有人会在任何实时代码中看到这个运算符,甚至可能不会添加它。最好等到细节最终确定并正确记录。 我们已经收到了有关它的问题。 :/ 如果提案失败,我们可以决定是否值得保留。 @JohnConde 我很想将它们关闭为题外话,因为他们在询问一种虚构的编程语言;或者“这个问题属于时间轴的不同部分,请检查你的时间机器上的设置”;) 大声笑这个想法掠过我的脑海。我问自己这是否会为未来的访客提供价值,目前,答案是“是”。但这可能会改变...... @JohnConde 问题是我们实际上还不能给出正确的答案——操作符的实际行为可能会完全改变,或者它可能永远不会被添加,所以我们现在写的任何东西都是可能的误导作为通知。唯一诚实的答案是“语法错误,但它有可能在未来意味着什么”。【参考方案20】:

NullSafe 运算符 "?->" 从 php8 开始

在 PHP8 中已经接受了这个新的操作符,你可以找到文档here。 ?-&gt;NullSafe Operator,如果您尝试调用函数或从 null 获取值,它会返回 null...

例子:

<?php
$obj = null;
$obj = $obj?->attr; //return null
$obj = ?->funct(); // return null
$obj = $objDrive->attr; // Error: Trying to get property 'attr' of non-object
?>

【讨论】:

添加这个感觉还为时过早,没有人会在任何实时代码中看到这个运算符,甚至可能不会添加它。最好等到细节最终确定并正确记录。 @IMSoP 我和你在一起,但是我被邀请在这里这样做***.com/questions/62178354/… ... 看起来我们现在有两个的答案。就个人而言,我会投票结束另一个问题而不是试图回答,因为现在没有这样的运营商。 @IMSoP 在 *** 上有很多关于 C++ 提案的问题,但它们还没有结束:尽管如此,我同意你暂时不在这里发布这个运算符 我不太了解 C++ 流程以及何时讨论提案是合理的,公平地说,这似乎很有可能成功修改锻炼,但总的来说有很多 为 PHP 提出的功能从未进入该语言,如果用户在参考列表中遇到所有这些功能,会相当混乱。也就是说,我在这里只是一个脾气暴躁的老人:P【参考方案21】:

PHP 数组:

数组是一种数据结构,在单个值中存储一个或多个相似类型的值

• 数字数组 - 具有数字索引的数组。值以线性方式存储和访问。

• 关联数组 - 以字符串为索引的数组。这会将元素值与键值关联起来,而不是按照严格的线性索引顺序来存储。

• 多维数组 - 包含一个或多个数组和值的数组使用多个索引访问 数值数组

这些数组可以存储数字、字符串和任何对象,但它们的索引将由数字表示。默认情况下,数组索引从零开始。

示例:

<html>
   <body>
   
      <?php
         /* First method to create array. */
         $numbers = array( 1, 2, 3, 4, 5);
         
         foreach( $numbers as $value ) 
            echo "Value is $value <br />";
         
         
         /* Second method to create array. */
         $numbers[0] = "one";
         $numbers[1] = "two";
         $numbers[2] = "three";
         $numbers[3] = "four";
         $numbers[4] = "five";
         
         foreach( $numbers as $value ) 
            echo "Value is $value <br />";
         
      ?>
      
   </body>
</html>

输出:

Value is 1 
Value is 2 
Value is 3 
Value is 4 
Value is 5 
Value is one 
Value is two 
Value is three 
Value is four 
Value is five

关联数组

关联数组在功能上与数值数组非常相似,但它们的索引不同。关联数组会将它们的索引作为字符串,以便您可以在键和值之间建立强关联。

示例:

<html>
   <body>
      
      <?php
         /* First method to associate create array. */
         $salaries = array("mohammad" => 2000, "qadir" => 1000, "zara" => 500);
         
         echo "Salary of mohammad is ". $salaries['mohammad'] . "<br />";
         echo "Salary of qadir is ".  $salaries['qadir']. "<br />";
         echo "Salary of zara is ".  $salaries['zara']. "<br />";
         
         /* Second method to create array. */
         $salaries['mohammad'] = "high";
         $salaries['qadir'] = "medium";
         $salaries['zara'] = "low";
         
         echo "Salary of mohammad is ". $salaries['mohammad'] . "<br />";
         echo "Salary of qadir is ".  $salaries['qadir']. "<br />";
         echo "Salary of zara is ".  $salaries['zara']. "<br />";
      ?>
   
   </body>
</html>

输出:

Salary of mohammad is 2000
Salary of qadir is 1000
Salary of zara is 500
Salary of mohammad is high
Salary of qadir is medium
Salary of zara is low

多维数组

一个多维数组主数组中的每个元素也可以是一个数组。并且子数组中的每个元素都可以是一个数组,以此类推。使用多个索引访问多维数组中的值。

示例

<html>
   <body>
      
      <?php
         $marks = array( 
            "mohammad" => array (
               "physics" => 35,
               "maths" => 30,   
               "chemistry" => 39
            ),
            
            "qadir" => array (
               "physics" => 30,
               "maths" => 32,
               "chemistry" => 29
            ),
            
            "zara" => array (
               "physics" => 31,
               "maths" => 22,
               "chemistry" => 39
            )
         );
         
         /* Accessing multi-dimensional array values */
         echo "Marks for mohammad in physics : " ;
         echo $marks['mohammad']['physics'] . "<br />"; 
         
         echo "Marks for qadir in maths : ";
         echo $marks['qadir']['maths'] . "<br />"; 
         
         echo "Marks for zara in chemistry : " ;
         echo $marks['zara']['chemistry'] . "<br />"; 
      ?>
   
   </body>
</html>

输出:

Marks for mohammad in physics : 35
Marks for qadir in maths : 32
Marks for zara in chemistry : 39

PHP 数组函数

array() -> 创建一个数组

array_change_key_case() -> 将数组中的所有键更改为小写或大写

array_chunk() -> 将数组拆分为数组块 array_column() -> 返回输入数组中单列的值

array_combine() -> 使用一个“keys”数组和一个“values”数组中的元素创建一个数组

array_count_values() -> 计算一个数组的所有值

array_diff() -> 比较数组,并返回差异(仅比较值)

array_diff_assoc() -> 比较数组,并返回差异(比较键和值)

array_diff_key() -> 比较数组,并返回差异(仅比较键)

array_diff_uassoc() -> 比较数组,并返回差异(比较键和值,使用用户定义的键比较函数)

array_diff_ukey() -> 比较数组,并返回差异(仅比较键,使用用户定义的键比较函数)

array_fill() -> 用值填充数组

array_fill_keys() -> 用值填充数组,指定键

array_filter() -> 使用回调函数过滤数组的值

array_flip() -> 翻转/交换数组中所有键及其关联值

array_intersect() -> 比较数组,并返回匹配项(仅比较值)

array_intersect_assoc() -> 比较数组并返回匹配项(比较键和值)

array_intersect_key() -> 比较数组,并返回匹配项(仅比较键)

array_intersect_uassoc() -> 比较数组,并返回匹配项(比较键和值,使用用户定义的键比较函数)

array_intersect_ukey() -> 比较数组,并返回匹配项(仅比较键,使用用户定义的键比较函数)

array_key_exists() -> 检查指定的键是否存在于数组中

array_keys() -> 返回一个数组的所有键

array_map() -> 将数组的每个值发送到用户自定义函数,该函数返回新值

array_merge() -> 将一个或多个数组合并为一个数组

array_merge_recursive() -> 递归地将一个或多个数组合并为一个数组

array_multisort() -> 对多个或多维数组进行排序

【讨论】:

以上是关于php函数参数中的&符号是啥意思的主要内容,如果未能解决你的问题,请参考以下文章

用 & 符号启动 PHP 函数是啥意思?

ultoa中参数radix是啥意思?

函数 cudaCreateChannelDesc 中的参数 x,y,z,w 是啥意思

PHP中的extract是啥作用??

包体中有两个同名但参数不同的函数是啥意思?

vs函数不接受一个参数是啥问题呢