开发者

function gives error on linux server

I'm using a function i found on php.net(i think) to sort an array based on a value

usort($comments, function ($a, $b) { return $b["date"] - $a["date"]; });

It's supposed to put newer dates first.Works just fine on windows localhost, gives error on l开发者_StackOverflow中文版inux server.Why ? Could anyone give me a replacement ?


You are probably using a PHP version < 5.3 on your Linux. Anonymous functions are only available on the latest PHP versions.

function mySort($a, $b) { return $b["date"] - $a["date"]; }

usort($comments, 'mySort');


Probably because your server does not run PHP 5.3 and lambda functions are only available since then. What error do you get?

In general, the code looks correct. A working version for PHP < 5.3 would be:

function custom_sort($a, $b) {
    return $b["date"] - $a["date"];
}

usort($comments, "custom_sort");


My guess: You Windows server is running PHP 5.3. Your Linux server is running an older version. Support for anonymous functions was added in PHP 5.3.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜