解析错误:语法错误,php 5.3 中出现意外的“[”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16358973/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 10:58:55  来源:igfitidea点击:

Parse error: syntax error, unexpected '[' with php 5.3

phpphp-5.3parse-error

提问by Owl

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

我的脚本在我的 xampp 上运行得非常好。现在我尝试上传到服务器上,但是它直接吐了一个

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)

我不知道出了什么问题。我试图查看 php 5.3 更新日志,但没有找到任何相关信息。(因为我在服务器上有 5.3,而在 xampp 上它是旧版本)

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

我很感激任何帮助,我无法使用 google 搜索 [ 并且不知道它可能来自哪里,因为在 xampp 上它工作正常

回答by Willy Pt

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

尝试这个 $item = $xml->xpath($path);
$item = $item[0];