PHP 版本 5.2.14 / 解析错误:语法错误,意外的 T_FUNCTION,期待 ')'
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3723748/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP Version 5.2.14 / Parse error: syntax error, unexpected T_FUNCTION, expecting ')'
提问by tzmatt7447
I have a certain piece of code that I'm trying to use with PHP Version 5.2.14 . Is it incompatible?? I run the following,
我有一段代码试图与 PHP 版本 5.2.14 一起使用。不兼容吗??我运行以下,
jailshell-3.2$ php -l /XYZ/functions.php
And it gives:
它给出:
Parse error: syntax error, unexpected T_FUNCTION, expecting ')' in /XYZ/functions.php on line 2115 Errors parsing /XYZ/functions.php
解析错误:语法错误,意外的 T_FUNCTION,在第 2115 行的 /XYZ/functions.php 中需要 ')' 解析 /XYZ/functions.php 错误
The code is:
代码是:
2114 $range = array_map(
2115 function (DatePeriod $p) use ($vt2) {
2116 $res = array();
回答by shamittomar
Your code uses anonymous functions
which were supported in PHP 5.3. So, you need PHP 5.3 to get it to working. Upgrade your server's PHP installation.
您的代码使用anonymous functions
了 PHP 5.3 支持的内容。因此,您需要 PHP 5.3 才能使其正常工作。升级服务器的 PHP 安装。
Anonymous functions, also known as closures, allow the creation of functions which have no specified name.
匿名函数,也称为闭包,允许创建没有指定名称的函数。
回答by codaddict
You are using anonymous functionswhich are available since PHP 5.3.0.
您正在使用自PHP 5.3.0起可用的匿名函数。
To resolve this you can upgrade your PHP as suggested in other answer.
要解决此问题,您可以按照其他答案中的建议升级您的 PHP。
Alternatively you can define the function outside array_map
and then use that function name in the call to array_map
或者,您可以在外部定义函数array_map
,然后在调用中使用该函数名称array_map
回答by Lee
From the php manual on Anonymous Functions:
来自匿名函数的 php 手册:
Note: Anonymous functions are available since PHP 5.3.0.
注意:匿名函数自 PHP 5.3.0 起可用。
prior to 5.3.0, do it like this:
在 5.3.0 之前,这样做:
$range = array_map( "name_of_function_to_call", $myArray );
回答by gianebao
I think the lambda style function is not yet implemented in 5.2
我认为 5.2 中还没有实现 lambda 风格的函数
use create_functionor just create the function and pass it the function name in array_map.
使用create_function或只是创建函数并将函数名称传递给array_map。