Sunday, November 3, 2019

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

hard drive - Leaving bad sectors in unformatted partition?

Laptop was acting really weird, and copy and seek times were really slow, so I decided to scan the hard drive surface. I have a couple hundr...