[英]Does php have an equivalent of javascript arrow functions (=>)?
I know the =>
symbol is used when constructing arrays in php.我知道在 php 中构造 arrays 时使用了
=>
符号。 But in javascript, you can use the symbol to shorten a function like this:但是在 javascript 中,您可以使用该符号来缩短 function,如下所示:
var materials = [
'Hydrogen',
'Helium',
'Lithium',
'Beryllium'
];
console.log(materials.map(material => material.length));
// expected output: Array [8, 6, 7, 9]
where material
would be the input of the function and material.length
the return value.其中
material
是 function 的输入, material.length
是返回值。
Is there's an equivalent in php? php 中是否有等价物?
In php7.4 arrow functions are implemented according to this rfc https://wiki.php.net/rfc/arrow_functions_v2在php7.4中箭头功能是根据这个rfc https://wiki.php.net/rfc/arrow_functions_v2实现的
Sample here: https://3v4l.org/ddooc , watch the results depending on php version.此处示例: https://3v4l.org/ddooc ,观察结果取决于 php 版本。
$array = ['string1', 'longstring'];
print_r(array_map(fn($x) => strlen($x), $array));
In older versions of php you still have to use:在旧版本的 php 中,您仍然必须使用:
array_map(
function ($x) {
return strlen($x);
},
$array
);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.