当前位置:网站首页>Excerpt of new features in PHP version - php7.1x
Excerpt of new features in PHP version - php7.1x
2022-07-18 10:48:00 【Chon. Wang】
This article excerpts from php.net
1. May be empty (Nullable) type
Parameters and the type of the return value can now be allowed to be empty by prefixing the type with a question mark . When this feature is enabled , The result returned by the passed parameter or function is either of the given type , Or null .
<?php
function testReturn(): ?string
{
return 'elePHPant';
}
var_dump(testReturn());
function testReturn(): ?string
{
return null;
}
var_dump(testReturn());
function test(?string $name)
{
var_dump($name);
}
test('elePHPant');
test(null);
test();
The above routine will output
string(10) "elePHPant"
NULL
string(10) "elePHPant"
NULL
Uncaught Error: Too few arguments to function test(), 0 passed in...
2. Class constant visibility
<?php
class ConstDemo
{
const PUBLIC_CONST_A = 1;
public const PUBLIC_CONST_B = 2;
protected const PROTECTED_CONST = 3;
private const PRIVATE_CONST = 4;
}
3. Symmetric array destructuring
Short array syntax (
[]) Now as list() An alternative to syntax , It can be used to assign the value of the array to some variables ( Included inforeachin ).
<?php
$data = [
[1, 'Tom'],
[2, 'Fred'],
];
// list() style
list($id1, $name1) = $data[0];
// [] style
[$id1, $name1] = $data[0];
// list() style
foreach ($data as list($id, $name)) {
// logic here with $id and $name
}
// [] style
foreach ($data as [$id, $name]) {
// logic here with $id and $name
}
4. list() Key names are now supported
Now? list() And its new
[]Syntax supports specifying key names within it . This means that it can put any type of array All assigned to variables ( Similar to short array syntax )
<?php
$data = [
["id" => 1, "name" => 'Tom'],
["id" => 2, "name" => 'Fred'],
];
// list() style
list("id" => $id1, "name" => $name1) = $data[0];
// [] style
["id" => $id1, "name" => $name1] = $data[0];
// list() style
foreach ($data as list("id" => $id, "name" => $name)) {
// logic here with $id and $name
}
// [] style
foreach ($data as ["id" => $id, "name" => $name]) {
// logic here with $id and $name
}
5. Support negative string offsets
Now all of the String manipulation functions Both support accepting negative numbers as offsets , Including passage
[]or{}operation String subscript . under these circumstances , A negative offset is interpreted as an offset starting at the end of the string .
use Operator It has been extended to support importing external functions and constants in classes . The corresponding structure is use function and use const.
<?php
var_dump("abcdef"[-2]);
var_dump(strpos("aabbcc", "b", -3));
$string = 'bar';
echo "The last character of '$string' is '$string[-1]'.\n";
The above routine will output
string (1) "e"
int(3)
The last character of 'bar' is 'r'.
边栏推荐
- 开发人员使用Helix QAC实现静态代码测试合规性的五大原因
- 记录一次坑爹内存泄漏排查经历
- 无心剑中译迈克尔《代数的定义》
- Loam_ Livox code reading and summary
- Ten minute quick learning flash framework
- 搭建Sky Hackathon参赛环境
- Information system project manager will recite the core examination site (43) expected monetary value (EMV)
- 异步 API 设计之扇入扇出模式
- Design of user integral function
- Deep understanding of modern web browsers (3)
猜你喜欢

Thinking about the research links of data governance projects

1. Mx6u system migration-3-uboot startup process

Three main reasons for cloud disruption

ACL access control list case (7.15)

刷题——笔记2

Reinforcement Learning 强化学习(一)
![[figure 1 understand RTC] comprehensive upgrade of the underlying framework of audio and video](/img/69/eebbd5dbac4d8d4e045664a31641a0.png)
[figure 1 understand RTC] comprehensive upgrade of the underlying framework of audio and video

以太网开发与测试,这一步你做对了吗 (2)

SQL中去重的三种方法,还有谁不会?
Zabbix+mysql fpmmm (MPM) monitoring
随机推荐
[detailed tutorial] a thorough article on mongodb aggregation query
互联网数据聚合
PHP版本新特性摘选 - PHP7.0X
C# 多线程CPU占用高 简单优化
In strict mode, the method of inserting mysql5.7 into the user table fails to create a new user, so how to modify the strict mode to make him insert successfully. How to change the all permission to s
Fan in and fan out mode of asynchronous API design
2022数学建模“五一杯”B题
数据治理项目调研环节思考
Codeforces Round #787 (Div. 3)
开发人员使用Helix QAC实现静态代码测试合规性的五大原因
51CTO interview
Go+mysql+redis+vue3 simple chat room, the second bullet: database links and operations
Three main reasons for cloud disruption
Seven uncovered industrial 4.0 misunderstandings
移动布局 -适配
多御安全浏览器怎么移除密码?
The title is too long. Replace it with an ellipsis
艾瑞泽5 GT曝光,雷克萨斯同款前脸,国产品牌又开始借鉴了?
进阶过程:程序员做项目的独立性
Advanced process: the independence of programmers to do projects