0

I've been creating a php script for a project and have been running it on my development server which is running PHP 5.4.17

I need to move it over to my production server which is running PHP 5.4.19

When using the array_map function on my development server I got the results I needed with no problem.

On my production server I get the parse error:

Parse error: syntax error, unexpected '[' in /path/to/script/ on line 219

My code used was:

$arr = array_map(
    function($results_titles, $results_image, $results_summary, $results_dates, $results_links) {
        return ['title' => $results_titles, 'image' => $results_image, 'summary' => $results_summary, 'date' => $results_dates, 'link' => $results_links];
    },
    $results_titles, $results_image, $results_summary, $results_dates, $results_links
);
hindmost
  • 7,125
  • 3
  • 27
  • 39
Sal B.
  • 77
  • 2
  • 12
  • 1
    You've confused PHP and JS syntax) In PHP array initialized with: `array(...)`, not `[...]` – hindmost Feb 18 '14 at 08:15
  • 1
    I don't remember since which version using `[array_elements]` was valid, maybe the problem is there, try using the classic structure: `return array('title => $result_titles, etc...);` I would keep using that syntax for a while, just in case xD – aleation Feb 18 '14 at 08:16
  • Are you 2000% sure it's running on 5.4 on your server? – deceze Feb 18 '14 at 08:18
  • @hind Nope, PHP supports this syntax now as well. – deceze Feb 18 '14 at 08:18
  • @hindmost since 5.4.0 you can use `[]` directly for the arrays too. OP, I think you are on a version prior to 5.4.0, check this link, your code should be valid on >5.4 http://php.net/manual/en/migration54.new-features.php – aleation Feb 18 '14 at 08:20
  • @deceze However the given error msg tells that `[]` syntax is invalid (on OP's env.) – hindmost Feb 18 '14 at 08:23
  • @hind Maybe so, but *generally* PHP supports that syntax! – deceze Feb 18 '14 at 08:45
  • BTW, @Sal: `function ($title, $image, $summary, $date, $link) { return compact('title', 'image', 'summary', 'date', 'link'); }` - much more concise. – deceze Feb 18 '14 at 08:47
  • @aleation your answer worked the best here. Thanks. I'm still not sure why it didn't work on my production server when phpinfo() output that version. But return array(); worked great. Thanks. – Sal B. Feb 18 '14 at 08:57

1 Answers1

2

The new array syntax comes up with PHP 5.4.

So make sure your php version in your server is >= PHP 5.4

Note: the cli and the web server(eg: apache) could run different version of php.

xdazz
  • 158,678
  • 38
  • 247
  • 274