0

I have been developing locally using Mamp, and everything was great until I uploaded to the server. I have narrowed my problem down to having to do with the php version. Mamp was running on a newer version than the server.

If I run Mamp on PHP 5.6.2(or 5.5.X) I have no problems with my code. But if all I do is change the PHP version in Mamp preferences to PHP 5.3.29 if complains about the following line of code:

$shipping = reset($arrShipOptions['options'])[0]['price'];

The error is:

syntax error, unexpected '['

First thing that came to mind was that reset() might be a new function. But according to http://php.net/manual/en/function.reset.php it was already available in PHP 4

Could an extra pair of eyes shed some light on this please. Thanks

Joshua Goossen
  • 1,714
  • 1
  • 17
  • 36

3 Answers3

3

In older PHP versions you have to assign result from reset (or any other function) to variable and then access it using [].

 $shipping = reset($arrShipOptions['options']);
 $shipping = $shipping[0]['price'];
Elon Than
  • 9,603
  • 4
  • 27
  • 37
  • 1
    Wow! I guess that was an easy question that just about everybody knew the answer to except me. Thanks for the help. +1 for being the quickest to answer. Will mark as correct when SO allows me to. – Joshua Goossen Jan 05 '15 at 16:11
0

before php 5.4 you cant chain syntax like that... http://docs.php.net/manual/en/language.types.array.php

It's called array dereferencing. It is not available in php 5.3

// on PHP 5.4
$secondElement = getArray()[1];

// previously
$tmp = getArray();
$secondElement = $tmp[1];
geggleto
  • 2,605
  • 1
  • 15
  • 18
0

The problem is caused by using a feature available form PHP 5.4+ called

Function array dereferencing

Source http://php.net/manual/en/migration54.new-features.php (the third feature)

The solution is breaking the code into two lines:

$shipping = reset($arrShipOptions['options']);
$shipping = $shipping[0]['price'];
Plamen Nikolov
  • 2,643
  • 1
  • 13
  • 24