19

My script is working really fine on my xampp. Now I tried to upload it on the server, but it spat directly a

Parse error: syntax error, unexpected '['

in my face. :(

The line which its mocking about is this one:

    $item = $xml->xpath($path)[0];

And I have no idea what is wrong. I tried to look on the php 5.3 changelog but did not found anything about it. (Because I have 5.3 on the server, and on xampp its an olderversion)

The whole code block looks like this:

$path = '//item[@id="'.$id.'"]';
if ($xml->xpath($path)) {
    $item = $xml->xpath($path)[0];
} else {
    die('<p class="error">Script Error: Code 101 - Please contact administrator</p>');
}

I am thankful for any help, I cannot seach [ with google and have no idea where it could come from, since on xampp its working fine

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
Owl
  • 689
  • 5
  • 15
  • 29
  • 9
    you need 5.4 to use it like this. in your case `$item = $xml->xpath($path); $item[0];` – Kin May 03 '13 at 12:32
  • 3
    *"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."* http://php.net/manual/en/language.types.array.php – Felix Kling May 03 '13 at 12:33
  • $item = $xml->xpath($path0); i don't know but does this work? – Kees Sonnema May 03 '13 at 12:33
  • You need to change `$item = $xml->xpath($path)[0];` to `$item = (string)$xml->xpath($path)[0]->value;` – Sumit Bijvani May 03 '13 at 12:37
  • I don't know why this question is duplicate of this : [PHP syntax for dereferencing function result](http://stackoverflow.com/questions/742764/php-syntax-for-dereferencing-function-result) – Sumit Bijvani May 03 '13 at 12:40

1 Answers1

37

Try this $item = $xml->xpath($path);
$item = $item[0];

Willy Pt
  • 1,795
  • 12
  • 20