Sunday, July 21, 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

plot explanation - Why did Peaches&#39; mom hang on the tree? - Movies &amp; TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...