Saturday 19 May 2018

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

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...