1

Why do I get this "Parse error: syntax error, unexpected T_FUNCTION in upload2.php on line 5" from this snippet:

<?php
$title = "Click to see the picture in full size";

$images = glob('./images/*.*', GLOB_BRACE); 
usort($images, function($a, $b) {  
return filemtime($a) < filemtime($b);  
});

foreach($images as $image) {
echo '<a href="'.$image.'"><img src="'.$image.'" width="430px" height="350px"    title="'.$title.'"></a>';
}

?>

It is working fine when I am using XAMPP localhost. Thanks in advance!

  • 1
    What version of PHP are you using? – Dave Chen Feb 02 '14 at 20:34
  • possible duplicate of [Parse error: syntax error, unexpected T\_FUNCTION line 10 ? help?](http://stackoverflow.com/questions/4949573/parse-error-syntax-error-unexpected-t-function-line-10-help) – jww Feb 03 '14 at 03:10

2 Answers2

5

You are running different versions of PHP. Your local version supports anonymous functions (5.3+) but your production version does not.

John Conde
  • 217,595
  • 99
  • 455
  • 496
0

The anonymous function syntax you're using on this line:

usort($images, function($a, $b) {  

is only available in PHP 5.3 and later. Your server is probably running PHP 5.2, which does not support this.

You will need to define the function separately and reference it by name, e.g.

function sort_by_mtime($a, $b) {
    ...
}

usort($images, "sort_by_mtime");