参考—;这个符号在PHP中是什么意思? 这是什么?
这是一个关于PHP语法的问题集合。这也是一个社区Wiki,因此邀请每个人参与维护此列表 为什么会这样? 过去很难找到关于运算符和其他语法标记的问题。ª参考—;这个符号在PHP中是什么意思? 这是什么?,php,arguments,operators,symbols,semantics,Php,Arguments,Operators,Symbols,Semantics,这是一个关于PHP语法的问题集合。这也是一个社区Wiki,因此邀请每个人参与维护此列表 为什么会这样? 过去很难找到关于运算符和其他语法标记的问题。ª 其主要思想是提供有关堆栈溢出的现有问题的链接,以便我们更容易参考它们,而不是复制PHP手册中的内容 注:自2013年1月起,堆栈溢出。只需将搜索词用引号括起来,例如 我应该在这里做什么? 如果有人在这里指你,因为你问了这样一个问题,请在下面找到特定的语法。链接到的页面以及链接的问题很可能会回答您的问题。如果是这样的话,我们鼓励你对答案投赞成票。此
其主要思想是提供有关堆栈溢出的现有问题的链接,以便我们更容易参考它们,而不是复制PHP手册中的内容 注:自2013年1月起,堆栈溢出。只需将搜索词用引号括起来,例如 我应该在这里做什么? 如果有人在这里指你,因为你问了这样一个问题,请在下面找到特定的语法。链接到的页面以及链接的问题很可能会回答您的问题。如果是这样的话,我们鼓励你对答案投赞成票。此列表并不能替代他人提供的帮助 名单 如果您的特定令牌未在下面列出,您可能会在中找到它
&
或
- (&$)
=&
&=
&&
%
代码>
@
?:
??
(从PHP 7开始)
?字符串
?int
?数组
?bool
?float
(从PHP 7.1开始)
:
:
\
->
=>
^
>>
++
增量运算符
--
减量运算符
示例名称效果
---------------------------------------------------------------------
++$a预增量将$a递增1,然后返回$a。
$a++Post increment返回$a,然后将$a增加1。
--$a预减量将$a减量1,然后返回$a。
$a—递减后返回$a,然后按1递减$a。
这些可以在变量之前或之后
如果将其置于变量之前,则先对变量执行递增/递减操作,然后返回结果。如果放在变量之后,首先返回变量,,然后执行递增/递减操作
例如:
$apples = 10;
for ($i = 0; $i < 10; ++$i) {
echo 'I have ' . $apples-- . " apples. I just ate one.\n";
}
一旦到达z
,aa
是下一个,依此类推
请注意,字符变量可以递增,但不能递减,即使如此,也只支持普通ASCII字符(a-z和a-z)
堆栈溢出帖子:
语法
名称
描述
x==y
平等
true
如果x和y具有相同的键/值对
x!=y
不平等
true
如果x不等于y
x==y
身份
true
如果x和y具有相同顺序和相同类型的相同键/值对
x!==y
非身份
true
如果x与y不相同
++x
增量前
将x增加1,然后返回x
x++
增量后
返回x,然后将x增加1
--x
预减量
将x减1,然后返回x
x--
后减量
返回x,然后将x减1
x和y
及
true
如果x和y都是true
。如果x=6,y=3,则
(x<10和y>1)
返回true
x&y
及
true
如果x和y都是true
。如果x=6,y=3,则
(x<10&&y>1)
返回true
x或y
或
true
如果x或y中的任何一个为true
。如果x=6,y=3,则
(x<10或y>10)
返回true
x|y
或
true
如果x或y中的任何一个为true
。如果x=6,y=3,则
(x<3|y>1)
返回true
a。b
串联
连接两个字符串:“Hi”。“哈”
位运算符
什么是一点点?位表示1或0。基本关闭(0)和打开(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”运算符:&
这将输出数字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
“或”运算符:|
这将输出数字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 = 36;
$b = 103;
echo $a & $b; // This would output the number 36.
$a = 00100100
$b = 01100111
$a = 9;
$b = 10;
echo $a | $b;
-------------------------------------------
| 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|
-------------------------------------------
<?php
class mclass { }
class sclass { }
$a = new mclass;
var_dump($a instanceof mclass);
var_dump($a instanceof sclass);
bool(true)
bool(false)
<?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)
1 <=> 1; // 0
1 <=> 2; // -1
2 <=> 1; // 1
$arr = [4,2,1,3];
usort($arr, function ($a, $b) {
if ($a < $b) {
return -1;
} elseif ($a > $b) {
return 1;
} else {
return 0;
}
});
$arr = [4,2,1,3];
usort($arr, function ($a, $b) {
return $a <=> $b;
// return -1 * ($a <=> $b); // for reversing order
});
// 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
$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
$string = 'This is my string'; // print This is my string
$str = 'string';
$string = "This is my $str"; // print This is my string
$string = <<<EOD
This is my string
EOD; // print This is my string
$string = <<<'END_OF_STRING'
This is my string
END_OF_STRING; // print This is my string
$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
$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;
echo $myArray1["Greetings"];// output: hello
echo $myArray2["Greetings"];// output: hello
echo $myArray3["Greetings"];// output: hello
<?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';
?>
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
echo $count ? $count : 10; // outputs 10
// $a is not set
$b = 16;
echo $a ?? 2; // outputs 2
echo $a ?? $b ?? 7; // outputs 16
<?php
function get_item(): ?string {
if (isset($_GET['item'])) {
return $_GET['item'];
} else {
return null;
}
}
?>
<?php
function calculateNumbers(...$params){
$total = 0;
foreach($params as $v){
$total = $total + $v;
}
return $total;
}
echo calculateNumbers(10, 20, 30, 40, 50);
//Output 150
?>
<?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
?>
$objDrive = null;
$drive = $objDrive?->func?->getDriver()?->value; //return null
$drive = $objDrive->func->getDriver()->value; // Error: Trying to get property 'func' of non-object
$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"
<?php
$obj = null;
$obj = $obj?->attr; //return null
$obj = ?->funct(); // return null
$obj = $objDrive->attr; // Error: Trying to get property 'attr' of non-object
?>