Join the Stack Overflow Community
Stack Overflow is a community of 6.4 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

This question already has an answer here:

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

share|improve this question

marked as duplicate by Gordon May 3 '13 at 12:34

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

7  
you need 5.4 to use it like this. in your case $item = $xml->xpath($path); $item[0]; – Kin May 3 '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." php.net/manual/en/language.types.array.php – Felix Kling May 3 '13 at 12:33
    
$item = $xml->xpath($path0); i don't know but does this work? – Kees Sonnema May 3 '13 at 12:33
    
You need to change $item = $xml->xpath($path)[0]; to $item = (string)$xml->xpath($path)[0]->value; – Sumit Bijvani May 3 '13 at 12:37
    
I don't know why this question is duplicate of this : PHP syntax for dereferencing function result – Sumit Bijvani May 3 '13 at 12:40
up vote 28 down vote accepted

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

share|improve this answer
2  
Or you could migrate to >= PHP 5.4.x – Junior M Nov 23 '15 at 12:41

Not the answer you're looking for? Browse other questions tagged or ask your own question.