1

I was running the following line on PHP5.4 without any problem:

$lstContent = $data->xpath("/response/lst[@name='highlighting']")[0]->lst[$i]->arr->str;

But now on PHP5.3 (production system) I get the following error:

Parse error: syntax error, unexpected '[' in /var/www/html/upload/inc_suche_code.php on line 153

Any ideas for a quick fix? Updating PHP won't work for me.

mrks
  • 5,439
  • 11
  • 52
  • 74

1 Answers1

6

In older versions of PHP you can not access array values directly on variables that are the result of a function. You have to split up the expression using a temporary variable.

$result = $data->xpath("/response/lst[@name='highlighting']");
$lstContent = $result[0]->lst[$i]->arr->str;

As of PHP 5.4 it is possible to array dereference the result of a function or method call directly. Before it was only possible using a temporary variable.

Source: http://php.net/manual/en/language.types.array.php

Edit: Obligatory "you should also consider upgrading your PHP version". This annoying limitation was fixed ages ago, not to mention that the 5.3 had its end of life in 2014, meaning it has not received security upgrades since.

OptimusCrime
  • 14,662
  • 13
  • 58
  • 96