1,
在调用函数的时候,使用 … 运算符, 将 数组 和 可遍历 对象展开为函数参数。 在其他编程语言,比如 Ruby中,这被称为连接运算符,。
1
2
3
4
5
6
7
8
<?php function add($a, $b, $c) { return $a + $b + $c; } $operators = [2, 3]; echo add(1, ...$operators); ?>以上例程会输出:
6
2,
debugInfo() (PHP 5 >= 5.6.0, PHP 7)
加入 debugInfo(), 当使用 var_dump() 输出对象的时候, 可以用来控制要输出的属性和值。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?php class C { private $prop; public function construct($val) { $this->prop = $val; } public function debugInfo() { return [ 'propSquared' => $this->prop ** 2, ]; } } var_dump(new C(42)); ?>以上例程会输出:
1
2
3
4
object©#1 (1) {
[“propSquared”]=>
int(1764)
}
3,
PHP 变量类型与返回值类型声明
标量类声明
默认情况下,所有的PHP文件都处于弱类型校验模式
PHP增加了标量类型声明的特征,标量类型声明有两种模:
强制模式
严格模式
标量类型声明语法格式:
declare(strict_types=1);
代码中通过制定strict_types的值(1或者0),1表示严格类型效验模式,作用于函数调用和返回语句;0表示弱列席效验模式。
可以使用的类型参数有:
int float bool string interfaces array callable
强制模式例如:
1 <?php
2
3 //强制模式
4
5 function sum(int …KaTeX parse error: Expected '}', got 'EOF' at end of input: …turn array_sum(ints);
8
9 }
10
11 print(sum(2,‘3’,4.1));
12
13 ?>
程序输出结果为9;
例子将参数4.1转为整数4后再想加;
严格模式例如:
1 <?php
2
3 //严格模式
4
5 declare(strice_types=1);
6
7 function sum(int …KaTeX parse error: Expected '}', got 'EOF' at end of input: …eturn array_sumints);
10
11 }
12
13 print(sum(2,‘3’,4.1);
14
15 ?>
程序采用了严格模式,所以参数中出现了不合适整数的类型就会报错,执行出结果为:
PHP Fatal error: Uncaught TypeError: Argument 2 passed to sum() must be of the type integer, string given, called in……
4,
返回类型声明
PHP 7 增加了对返回类型声明的支持,返回类型声明指明了函数返回值的类型。
可以声明的返回类型有:
int float bool string interfaces array callable
例如:
<?php declare(strict_types=1); function returnIntValue(int $value): int { return $value; } print(returnIntValue(5)); ?>程序输出结果为5
5,
PHP NULL 合并运算符
PHP 7 新增加的 NULL 合并运算符(??)是用于执行isset()检测的三元运算的快捷方式。
NULL 合并运算符会判断变量是否存在且值不为NULL,如果是,它就会返回自身的值,否则返回它的第二个操作数。
以前我们这样写三元运算符:
1 <?php
2 // 获取 $_GET[‘site’] 的值,如果不存在返回 ‘运算符’
3 $site = GET[′site′]??′运算符′;45print(_GET['site'] ?? '运算符';
4
5 print(GET[′site′]??′运算符′;45print(site);
6 print(PHP_EOL); // PHP_EOL 为换行符
7
8
9 // 以上代码等价于
10 site=isset(site = isset(site=isset(_GET[‘site’]) ? GET[′site′]:′运算符′;1112print(_GET['site'] : '运算符';
11
12 print(GET[′site′]:′运算符′;1112print(site);
13 print(PHP_EOL);
14
15 $site = $_GET[‘site’] ?? POST[′site′]??′运算符′;1617print(_POST['site'] ?? '运算符';
16
17 print(POST[′site′]??′运算符′;1617print(site);
18 ?>
程序执行输出结果为
运算符
运算符
运算符
6,
太空船操作符(组合比较符)(PHP 7)
太空船操作符用于比较两个表达式。当a小于、等于或大于a小于、等于或大于a小于、等于或大于b时它分别返回-1、0或1。 比较的原则是沿用 PHP 的常规比较规则进行的。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<?php // Integers echo 1 <=> 1; // 0 echo 1 <=> 2; // -1 echo 2 <=> 1; // 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 ?>7,
通过 define() 定义常量数组 (PHP 7)
Array 类型的常量现在可以通过 define() 来定义。在 PHP5.6 中仅能通过 const 定义。
1
2
3
4
5
6
7
8
9
<?php define('ANIMALS', [ 'dog', 'cat', 'bird' ]); echo ANIMALS[1]; // outputs "cat" ?>8,
Buid-in web server内置了一个简单的Web服务器
把当前目录作为Root Document只需要这条命令即可:
php -S localhost:3300
也可以指定其它路径
php -S localhost:3300 -t /path/to/root
还可以指定路由
php -S localhost:3300 router.php
9,
Group use declarations
从同一 namespace 导入的类、函数和常量现在可以通过单个 use 语句 一次性导入了。
//PHP7之前
use some\namespace\ClassA;
use some\namespace\ClassB;
use some\namespace\ClassC as C;
use function some\namespace\fn_a;
use function some\namespace\fn_b;
use function some\namespace\fn_c;
use const some\namespace\ConstA;
use const some\namespace\ConstB;
use const some\namespace\ConstC;
// PHP7之后
use some\namespace{ClassA, ClassB, ClassC as C};
use function some\namespace{fn_a, fn_b, fn_c};
use const some\namespace{ConstA, ConstB, ConstC};
10,
魔术方法__callStatic()和__invoke()
PHP中原本有一个魔术方法__call(),当代码调用对象的某个不存在的方法时该魔术方法会被自动调用。新增的__callStatic()方法则只用于静态类方法。当尝试调用类中不存在的静态方法时,__callStatic()魔术方法将被自动调用。
class MethodTest {
public function __call($name, $arguments) {
// 参数 name大小写敏感echo“调用对象方法‘name 大小写敏感
echo “调用对象方法 ‘name大小写敏感echo“调用对象方法‘name’ “
. implode(‘ — ‘, KaTeX parse error: Expected 'EOF', got '\n' at position 14: arguments). “\̲n̲”;
}
/** PHP 5.…name, $arguments) {
// 参数 name大小写敏感echo“调用静态方法‘name 大小写敏感
echo “调用静态方法 ‘name大小写敏感echo“调用静态方法‘name’ “
. implode(‘ — ‘, $arguments). “\n”;
}
}
$obj = new MethodTest;
$obj->runTest(‘通过对象调用’);
MethodTest::runTest(‘静态调用’); // As of PHP 5.3.0
以上代码执行后输出如下:
调用对象方法’runTest’ –- 通过对象调用调用静态方法’runTest’ –- 静态调用
11,
Json更懂中文(JSON_UNESCAPED_UNICODE)
echo json_encode(“中文”, JSON_UNESCAPED_UNICODE);
//输出:“中文”
12,
参数跳跃
如果你有一个函数接受多个可选的参数,目前没有办法只改变最后一个参数,而让其他所有参数为默认值。
RFC上的例子,如果你有一个函数如下:
function create_query($where, $order_by, $join_type=’’, $execute = false, reporterrors=true)...那么有没有办法设置report_errors = true) { ... } 那么有没有办法设置reporterrors=true)...那么有没有办法设置report_errors=false,而其他两个为默认值。为了解决这个跳跃参数的问题而提出:
create_query(“deleted=0”, “name”, default, default, false);
13,
foreach 支持list()
对于“数组的数组”进行迭代,之前需要使用两个foreach,现在只需要使用foreach + list了,但是这个数组的数组中的每个数组的个数需要一样。看文档的例子一看就明白了。
array=[[1,2],[3,4],];foreach(array = [
[1, 2],
[3, 4],
];
foreach (array=[[1,2],[3,4],];foreach(array as list($a, $b)) {
echo “A: $a; B: $b\n”;
}
输出:
A: 1; B: 2
A: 3; B: 4
14,
短数组语法 Symmetric array destructuring
短数组语法([])现在可以用于将数组的值赋给一些变量(包括在foreach中)。 这种方式使从数组中提取值变得更为容易。
$data = [
[‘id’ => 1, ‘name’ => ‘Tom’],
[‘id’ => 2, ‘name’ => ‘Fred’],
];
while ([‘id’ => $id, ‘name’ => $name] = $data) {
// logic here with $id and $name
}
15,
intdiv()
接收两个参数作为被除数和除数,返回他们相除结果的整数部分。
var_dump(intdiv(7, 2));
输出int(3)
16,
empty() 支持任意表达式
empty() 现在支持传入一个任意表达式,而不仅是一个变量
function always_false() {
return false;
}
if (empty(always_false())) {
echo ‘This will be printed.’;
}
if (empty(true)) {
echo ‘This will not be printed.’;
}
输出
This will be printed.
17,
array_column
//从数据库获取一列,但返回是数组。
userNames=[];foreach(userNames = [];
foreach (userNames=[];foreach(users as $user) {
$userNames[] = $user[‘name’];
}
//以前获取数组某列值,现在如下
userNames=arraycolumn(userNames = array_column(userNames=arraycolumn(users, ‘name’);
未完待续…