使用 json_encode() 时解析短数组语法时出错


Error parsing short array syntax when using json_encode()

我正在与某个团队分享我的工作。在我这边,这条线json_encode(['Succeeded'])完美地工作,在团队方面,这条线失败了,并显示此消息

Parse error: syntax error, unexpected '[', expecting ')' in /home/app/file.php on line *

为什么会这样?

我想你可能安装了两个不同版本的PHP。

版本 5.4.0 中引入了短数组语法。 见 http://php.net/manual/en/migration54.new-features.php

因此,以下内容可能适用于两者:

json_encode(array('Succeeded'));

该语法仅在 php 5.4.0 开始受支持:

http://php.net/releases/5_4_0.php