什么是 PHP 中的尾随逗号?
尾随逗号自 PHP 7.2 起就已使用。我们可以在数组中的最后一个元素中使用尾随逗号。如果行已经使用了尾随逗号,我们可以在不修改最后一行的前提下添加数组元素。
PHP 8.0 前的尾随逗号
在 PHP 8 之前,我们无法在最后一个参数的末尾添加逗号。
示例
function($x,$y,$z){
}在 PHP 8.0 中
在 PHP 8 中,我们可以在最后一个参数的末尾添加尾随逗号。PHP 8 允许在参数列表和闭包的 use 列表中使用尾随逗号。
示例
function($x,$y,$z,){}示例:在 PHP 的函数、方法和闭包调用中使用尾随逗号。
<?php
function EmployeeAdd(string $country, string $city, string $street): string {
return $country . ', ' . $city. ', ' . $street;
}
$result = employeeAdd(
'India',
'Bangalore',
'Indira Nagar',
);
print_r($result);
?>输出
India, Bangalore, Indira Nagar
示例:带有多个参数的尾随逗号 PHP 8
<?php
function method_with_many_arguments(
$x,
$y,
$z,
)
{
var_dump("shows valid syntax");
}
method_with_many_arguments(
1,
2,
3,
4,
);
?>输出
string(18) "shows valid syntax"
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP