Friday 1 July 2016

php - unexpected T_FUNCTION error




I am using this function



    usort($arr, function($a, $b) use ($str) {return (strpos($a, $str) - strpos($b, $str));});


to sort the array $arr. This function works fine on localhost however when I uploaded my site online this error comes up





Parse error: syntax error, unexpected T_FUNCTION




Does anyone know why this is happening?


Answer



seems like your PHP version on your host is < 5.3.0
and its not support anonymous functions



http://php.net/manual/en/functions.anonymous.php




you may try this



function cmp($a, $b, $str) {
return (strpos($a, $str) - strpos($b, $str));
}

usort($arr, create_function('$a, $b', 'return cmp($a, $b, "' . $str . '");'));



or upgrade you php on server.


No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...