Reference — What does this symbol mean in PHP?
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.
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.
Note: Since January 2013, Stack Overflow does support special characters. Just surround the search terms by quotes, e.g. [php] "==" vs "==="
What should I do here?
如果您因为提出这样的问题而被某人指向此处,请在下面找到具体的语法.PHP手册的链接页面以及链接的问题可能会回答您的问题.如果是这样,我们鼓励您提出答案.此列表并不代替其他人提供的帮助.
列表
如果您的特定令牌未在下面列出,您可能会在" 解析器令牌列表"中找到它.
&
按位运算符或引用
- 使用&符号启动PHP函数意味着什么?
- 理解PHP&(&符号,按位和)运算符
- PHP"&"运算符
- PHP中&和&&之间的区别
- PHP中的"&"是什么意思?
- 在这种情况下,"&"是什么意思?
- PHP中"&"符号的含义是什么?
- 这个签名在PHP中意味着什么(&)?
- "&"运算符如何在PHP函数中工作?
- PHP中的&in和2意味着什么?
- 我什么时候应该使用按位运算符?
- 是否需要在物体前面使用&符号?($)
=&
参考
- PHP中的引用赋值运算符,=&
- PHP中的"=&"和"&="运算符是什么意思?
- '&='和'=&'运算符有什么作用?
- PHP中的=&表示什么?
&=
按位运算符
- PHP中的"=&"和"&="运算符是什么意思?
- '&='和'=&'运算符有什么作用?
&&
逻辑运算符
- 'AND'vs'&&'作为运营商
- PHP中&和&&之间的区别
- PHP中的"和"与"&&"运算符有什么区别吗?
- PHP - 和/或关键字
%
算术运算符
- 百分号在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?
?:
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 (since PHP 7)
- C#'s null coalescing operator (??) in PHP
?string
?int
?array
?bool
?float
Nullable return type declaration (since PHP 7.1)
- Nullable return type declaration
:
Alternative syntax for control structures, Ternary Operator
- What is ":" in PHP?
- What does ":" mean in PHP?
::
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+)?
->
Classes And Objects
- What is the "->" PHP operator called and how do you say it when reading code out loud?
- Where do we use the object operator "->" in PHP?
- What's the difference between :: (double colon) and -> (arrow) in PHP?
- What does the PHP syntax $var1->$var2 mean?
- What does "->" mean/refer to in PHP?
=>
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?
>>
Bitwise Operators
- What does >> mean in PHP?
<<
Bitwise Operators
- Strange print behaviour in PHP?
<<<
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 !=
<>
Comparison Operators
- PHP operator <>
- PHP's <> operator
- What is the difference between <> and !=
- Type-juggling and (strict) greater/lesser-than comparisons in PHP
<=>
Comparison Operators (since PHP 7.0)
- 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?
+=
and -=
Assignment Operators
- What is += used for?
- What does `$page -= 1` in my PHP document mean?
++
and --
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?
<?=
Short Open Tags
- What does this symbol mean in PHP <?=
- What does '<?=' mean in PHP?
- What does <?= mean?
[]
Arrays (short syntax since 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 = []
回答
递增/递减运算符
++
增量运算符
--
递减算子
Example Name Effect
---------------------------------------------------------------------
++$a Pre-increment Increments $a by one, then returns $a.
$a++ Post-increment Returns $a, then increments $a by one.
--$a Pre-decrement Decrements $a by one, then returns $a.
$a-- Post-decrement Returns $a, then decrements $a by one.
这些可以在变量之前或之后.
如果变量之前说,该递增/递减操作完成的变量第一,然后返回结果.如果放在变量之后,首先返回变量,然后完成递增/递减操作.
例如:
$apples = 10;
for ($i = 0; $i < 10; ++$i) {
echo 'I have ' . $apples-- . " apples. I just ate one.n";
}
实例
在使用上面的情况下++$i
,因为它更快.$i++
会有相同的结果.
预增量稍微快一些,因为它确实增加了变量,然后'返回'结果.后增量创建一个特殊变量,在那里复制第一个变量的值,并且只有在使用第一个变量之后,才用第二个变量替换它的值.
但是,您必须使用$apples--
,因为首先,您要显示当前的苹果数,然后您想从中减去一个苹果.
您还可以在PHP中增加字母:
$i = "a";
while ($i < "c") {
echo $i++;
}
下一次z
到达aa
,依此类推.
Stack Overflow帖子:
- 了解增量
- 为了每个人的利益,请删除有关预增量无限快的粗体信息.这是过早优化的绝对最糟糕的例子,如果他们刚开始学习PHP,这种信息不应该在人们的头脑中.
- @Lotus - 我认为这很有趣.如果你是PHP或C++等的初学者,那么++ i和i ++的不同就足以让它以不同的速度工作了.我发现它很吸引人.
- @Peter Ajtai是的,这很有意思,但是从你构建帖子的方式来看,你觉得它似乎是使用这种语言绝对至关重要的PHP的主要事实之一.
- 注释的+1表示减法者不对字符起作用,仅对数字起作用
- 不确定它是否只是我,但苹果的例子似乎有点误导.'我有10个苹果.我只是吃了一个'让我相信你有11个苹果,但现在你有10个因为你只吃了一个而不是你吃了一个后说你有10个,因此让你有9个 - 这就是代码所暗示的.
按位运算符
有点什么?位是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字节)
一些更好理解的例子
"AND"运算符: &
$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,该怎么办?
- 为什么`and &`是`0 0 0 0 1 0 0 0`而`or |`是`0 0 0 0 1 0 1 1`?
_
gettext()的别名
下划线字符'_'就像_()
是gettext()
函数的别名.
- @SherylHohman Look like it is translation function. https://developer.wordpress.org/reference/functions/__/
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"
<=>
宇宙飞船运营商
在PHP 7中添加
该飞船操作 <=>
是在PHP 7增加了最新的运营商相比,它是一种非关联二元运算符具有相同优先级的平等运算符(==
,!=
,===
,!==
).该运算符允许在左手操作数和右手操作数之间进行更简单的三向比较.
运算符导致整数表达式为:
-
0
当两个操作数相等时 - 小于
0
左操作数小于右操作数的时候 - 大于
0
左侧操作数大于右侧操作数的时间
例如
1 <=> 1; // 0
1 <=> 2; // -1
2 <=> 1; // 1
使用此运算符的一个很好的实际应用是比较类型的回调,预期基于两个值之间的三向比较返回零,负或正整数.传递给的比较函数usort
就是一个这样的例子.
在PHP 7之前你会写...
$arr = array(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;
});
- @mcrumley不,比这更糟糕.一般来说`$ a - $ b`甚至不适用于数字; 它只适用于*整数*.它不适用于非整数,因为`usort`将比较器函数的返回值强制转换为`int`,这意味着0.5会被强制转换为0,这意味着两个数字的差值小于1,例如4和4.6,可能(取决于哪一个作为比较器函数的第一个参数传递)错误地比较为相等.
- @AcidShout` $ a - $ b`适用于数字,但不适用于字符串,对象或数组.
- 不确定`$ a <=> $ b`与`$ a - $ b`的区别
- @MarkAmery这里的观点是不允许人们依赖未定义的行为.对于某些人获得的值不完全为1,0或-1的情况,您会收到某人提交错误报告,认为该语言存在问题.这就是为什么我们记录所有我们可以保证的值是__,_greater than_,或_equal to_ 0,而不一定是1,0和-1.
- @MarkAmery迁移指南不是运营商记录的行为.为此你想看一下http://www.php.net/language.operators.comparison手册的语言操作符部分,这背后的实际行为依赖于API的各种比较功能,比如当你是为字符串执行strcmp,在每种情况下都无法保证实际的返回值.当然,它几乎总是1,0或-1,但对于你不能保证它包装libc的strcmp的情况,你提供与底层规范相同的定义行为是安全的
- 谢里夫(Sherif),为什么要说明运算符返回“小于0”,0或“大于0”,而不是简单地说它返回1、0或-1?后者更精确,更简洁。我不明白您为什么要还原我的编辑内容。
- 顺便说一句,在 PHP7 之前,还有一种方法可以避免 if-else 块,它不仅适用于数字:`return $a < $b ? -1 : $a != $b;`
魔术常数:虽然这些不仅仅是符号,而是这个象征家族的重要组成部分.有八个魔法常数会根据它们的使用位置而改变.
__LINE__
:文件的当前行号.
__FILE__
:文件的完整路径和文件名.如果在include中使用,则返回包含文件的名称.从PHP 4.0.2开始,__FILE__
始终包含已解析符号链接的绝对路径,而在旧版本中,它在某些情况下包含相对路径.
__DIR__
:文件的目录.如果在include中使用,则返回包含文件的目录.这相当于dirname(__FILE__)
.除非它是根目录,否则此目录名称没有尾部斜杠.(在PHP 5.3.0中添加.)
__FUNCTION__
:函数名称.(在PHP 4.3.0中添加)从PHP 5开始,此常量返回声明的函数名称(区分大小写).在PHP 4中,它的值总是小写的.
__CLASS__
:班级名称.(在PHP 4.3.0中添加)从PHP 5开始,此常量返回声明的类名(区分大小写).在PHP 4中,它的值总是小写的.类名包括它声明的名称空间(例如FooBar
).请注意,从PHP 5.4开始,它__CLASS__
也适用于特征.在trait方法中使用时,__CLASS__
是使用特征的类的名称.
__TRAIT__
:特征名称.(在PHP 5.4.0中添加)从PHP 5.4开始,此常量返回声明的特征(区分大小写).特征名称包括声明它的名称空间(例如FooBar
).
__METHOD__
:类方法名称.(在PHP 5.0.0中添加)方法名称在声明时返回(区分大小写).
__NAMESPACE__
:当前命名空间的名称(区分大小写).此常量在编译时定义(在PHP 5.3.0中添加).
资源
类型运算符
instanceof
用于确定PHP变量是否是某个类的实例化对象.
<?php
class mclass { }
class sclass { }
$a = new mclass;
var_dump($a instanceof mclass);
var_dump($a instanceof sclass);
上面的例子将输出:
bool(true)
bool(false)
原因:上面的例子$a
是一个对象,mclass
所以只使用一个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)
bool(true)
bool(true)
克隆示例
<?php
class cloneable { }
$a = new cloneable;
$b = clone $a;
var_dump($a instanceof cloneable);
var_dump($b instanceof cloneable);
上面的例子将输出:
- 以上也适用于'接口'.这对于检查特定接口是否可用非常有用.
PHP中运算符的概述:
逻辑运算符:
- $ a && $ b:如果$ a和$ b都为TRUE,则为TRUE.
- $ a || $ b:如果$ a或$ b为TRUE,则为TRUE.
- $ a xor $ b:如果$ a或$ b为TRUE,则为TRUE,但不是两者都是.
- !$ a:如果$ a不为TRUE,则为TRUE.
- $ a和$ b:如果$ a和$ b都为TRUE,则为TRUE.
- $ a或$ b:如果$ a或$ b为TRUE,则为TRUE.
比较运算符:
- $ a == $ b:如果$ j在类型杂耍后等于$ b,则为TRUE.
- $ a === $ b:如果$ a等于$ b,则为TRUE,它们属于同一类型.
- $ a!= $ b:如果在类型杂耍后$ a不等于$ b,则为TRUE.
- $ a <> $ b:如果在类型杂耍之后$ a不等于$ b,则为TRUE.
- $ a!== $ b:如果$ a不等于$ b,则为TRUE,或者它们的类型不同.
- $ a <$ b:如果$ a严格小于$ b,则为TRUE.
- $ a> $ b:如果$ a严格大于$ b,则为TRUE.
- $ a <= $ b:如果$ a小于或等于$ b,则为TRUE.
- $ a> = $ b:如果$ a大于或等于$ b,则为TRUE.
- $ a <=> $ b:当$ a分别小于,等于或大于$ b时,小于,等于或大于零的整数.从PHP 7开始提供.
- $ a?$ b:$ c:如果$ a返回$ b,则返回$ c(三元运算符)
- $ a ?? $ c:与$ a相同?$ a:$ c(null合并运算符 - 需要PHP> = 7)
算术运算符:
- - $ 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'th权限的结果(在PHP 5.6中引入)
递增/递减运算符:
- ++ $ a:将$ a递增1,然后返回$ a.
- $ a ++:返回$ a,然后将$ a递增1.
- - $ a:将$ a减1,然后返回$ a.
- $ a--:返回$ a,然后将$ a减1.
按位运算符:
- $ a和$ b:设置$ a和$ b中设置的位.
- $ a | $ b:设置在$ a或$ b中设置的位.
- $ a ^ $ b:设置$ a或$ b但不是两者都设置的位.
- 〜$ a:未设置$ a中设置的位,反之亦然.
- $ a << $ b:将$ a $ b步的位移到左边(每一步意味着"乘以2")
- $ a >> $ b:向右移动$ a $ b步的位(每一步意味着"除以2")
字符串运算符:
- $ a.$ b:$ a和$ b的连接.
数组运算符:
- $ a + $ b:$ a和$ b的联盟.
- $ a == $ b:如果$ a和$ b具有相同的键/值对,则为TRUE.
- $ a === $ b:如果$ a和$ b在相同的顺序和相同类型中具有相同的键/值对,则为TRUE.
- $ a!= $ b:如果$ a不等于$ b,则为TRUE.
- $ a <> $ b:如果$ a不等于$ b,则为TRUE.
- $ a!== $ b:如果$ a与$ b不同,则为TRUE.
分配运营商:
- $ a = $ b:$ b的值分配给$ a
- $ a + = $ b:与$ a = $ a + $ b相同
- $ a - = $ b:与$ a = $ a - $ b相同
- $ a*= $ b:与$ a = $ a*$ b相同
- $ a/= $ b:与$ a = $ a/$ b相同
- $ a%= $ b:与$ a = $ a%$ b相同
- $ a**= $ b:与$ a = $ a**$ b相同
- $ a.= $ b:与$ a = $ a相同.$ b
- $ a&= $ b:与$ a = $ a和$ b相同
- $ a | = $ b:与$ a = $ a |相同 $ b
- $ a ^ = $ b:与$ a = $ a ^ $ b相同
- $ a << = $ b:与$ a = $ a << $ b相同
- $ a >> = $ b:与$ a = $ a >> $ b相同
注意
and
运算符和or
运算符的优先级低于赋值运算符=
.
这意味着$a = true and false;
相当于($a = true) and false
.
在大多数情况下,您可能希望使用&&
and ||
,其行为方式可以从C,Java或JavaScript等语言中获得.
宇宙飞船运营商<=>
(PHP 7中添加)
<=>
Spaceship运算符的示例(PHP 7,来源:PHP手册):
整数,浮点数,字符串,数组和对象用于变量的三向比较.
// 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
{}
大括号
- 积木 - 花括号/没有花括号?
- PHP中的字符串中的大括号
- 数组表示法中的PHP花括号
关于最后一篇文章的一些话
$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
- {''}属性名称的表示法.echo $ movies-> movie - > {'great-lines'} - > line; 可用于访问SimpleXMLElement中的数据.也许它也可以在其他情况下使用.http://php.net/manual/en/simplexml.examples-basic.php
空结合运算符(??)
此操作符已在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';
?>
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
题:
什么=>
意思?
回答:
=>
我们人类决定使用的符号来分离"Key" => "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]
数组是值一年 2016年,指数[1]
的阵列是一个问候,和索引[2]
数组是一个简单的整数值.我们将拥有的另一种选择是使用所谓的关联数组.关联数组与顺序数组有一些差异
(这是先前的情况,因为它们增加了预定序列中使用的索引,通过对每个后续值递增1).
差异(顺序和关联数组之间):
例如:
$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
最后一点:
因此,从上面的例子中,很容易看出该=>
符号用于表示数组中每个数组key
和数组之间的关联数组的关系.value
在数组内的值的启动期间.
问题:
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
==
用于检查相等性而不考虑可变数据类型
===
用于检查平等两个变量值*和**数据类型
例
$a = 5
Null Coalesce运算符"??" (在PHP 7中添加)
不是运营商最糟糕的名字,但PHP 7引入了相当方便的空合并,所以我想我会分享一个例子.
在PHP 5中,我们已经有一个三元运算符,它测试一个值,然后返回第二个元素,如果返回true,则返回第三个元素,如果不是:
echo $count ? $count : 10; // outputs 10
还有一个简写,它允许你跳过第二个元素,如果它与第一个元素相同:echo $ count?:10; //也输出10
在PHP 7中我们另外得到?? 运算符,而不是表示极端混乱,而不是我通常会一起使用两个问号,而是允许我们将一串值链接在一起.从左到右读取,存在且不为null的第一个值是将返回的值.
// $a is not set
$b = 16;
echo $a ?? 2; // outputs 2
echo $a ?? $b ?? 7; // outputs 16
此构造对于为来自用户输入或现有配置的一个或多个值赋予优先级非常有用,并且如果缺少该配置,则安全地回退到给定的默认值.这是一个小功能,但我知道,只要我的应用程序升级到PHP 7,我就会使用它.
三个 DOTS 作为 Splat 运算符 (...)(自 PHP 5.6 起)
PHP 有一个运算符“...”(三个点),称为 Splat 运算符。它用于在函数中传递任意数量的参数,这种类型的函数称为可变函数。让我们举个例子来使用“...”(三个点)。
<?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。
有许多不同的方法可以使用“...”运算符。下面举几个例子:
<?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
?>
<?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
?>
<?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
?>
可空的返回类型声明
PHP 7添加了对返回类型声明的支持。与参数类型声明类似,返回类型声明指定将从函数返回的值的类型。可用于返回类型声明的类型与用于参数类型声明的类型相同。
严格类型化也会对返回类型声明产生影响。在默认的弱模式下,如果返回的值还不是该类型,则将其强制为正确的类型。在强模式下,返回值必须是正确的类型,否则将引发TypeError。
从PHP 7.1.0开始,可以通过在类型名称前添加问号(?)来将返回值标记为可为空。这表示该函数返回指定的类型或NULL。
<?php
function get_item(): ?string {
if (isset($_GET['item'])) {
return $_GET['item'];
} else {
return null;
}
}
?>
资源
?-> 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"
NullSafe 运算符“?->”(可能)自 php8 起
在 PHP8 中它被接受了这个新的操作符,你可以在这里找到文档。?->
它是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
?>