Collin Clay
Collin Clay

Reputation: 1

Parse error: syntax error, unexpected T_FUNCTION upon calling uksort() with an anonymous function using rocketeer on PHP below 5.3

I am getting the parse error: "syntax error, unexpected T_FUNCTION" when calling uksort() with an anonymous callback function.

uksort($actions, function($a, $b) {
    if (strlen($a) == strlen($b)) {
        return 0;
    }
    if (strlen($a) > strlen($b)) {
        return -1;
    }
    return 1;
}

What's wrong here?

Upvotes: -2

Views: 199

Answers (2)

tuxtimo
tuxtimo

Reputation: 2790

function cmp($a, $b){
    if(strlen($a) == strlen($b)) {
        return 0;
    }
    if(strlen($a) > strlen($b)) {
        return -1;
    }
    return 1;
 }

 uksort( $actions, "cmp" );

You cannot use closures because your version must be newer or equal 5.3... That's the reason why you have to pass the function name as a string ;)

Upvotes: 0

deceze
deceze

Reputation: 522523

You are running a version of PHP older than 5.3, in which anonymous functions did not exist.

Upvotes: 0

Related Questions