-1

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?

user3741635
  • 852
  • 6
  • 16

1 Answers1

1

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.

M0rtiis
  • 3,676
  • 1
  • 15
  • 22
  • The answer you suggested works fine but what do you mean by upgrade the php on server. Can I upgrade the server or my hosting account if I don't own the server? – user3741635 Sep 21 '15 at 04:41
  • if you use hosting, you can only ask for that a hoster. in 99% they will not do that. you can change your hoster – M0rtiis Sep 21 '15 at 04:43
  • I would highly recommend changing your host if they only offer a version of PHP which has not been supported since the 6th of Janurary **2011** (PHP 5.2) – Scopey Sep 21 '15 at 04:53
  • Most shared hosting providers actually support switching (e.g. SetHandler) between multiple PHP versions (in particular for FPM or suphp setups). It's just that the older is commonly default. – mario Sep 21 '15 at 05:02