PHP的方括号语法错误 [英] Php square brackets syntax error

查看:113
本文介绍了PHP的方括号语法错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的本地[PHP版本5.5.9-1ubuntu4.5]这个code工作:

on my localhost [PHP Version 5.5.9-1ubuntu4.5] this code is working:

array($userName => ['score' => $score]);

和也是这个code工作:

and also this code is working:

$this->Auth->user()['id']

但生产服务器上[PHP版本5.3.3-7 + squeeze23]在这两种情况下,我得到了一个错误:

but on production server [PHP Version 5.3.3-7+squeeze23] in both cases I've got an error:

错误:致命错误(4):语法错误,意想不到的'['

Error: Fatal Error (4): syntax error, unexpected '['

这是怎么回事?如何解决它在一个简单的方法? (原因改变所有阵列中的项目是非常不切实际,我甚至不知道如何与验证...管理第二种情况下)

what's going on? how can I fix it in a simplest way? (cause changing all arrays in project is highly impracticable and I'm even not sure how to manage the second case with Auth...)

推荐答案

首先是因为新的 [] 实例化数组语法只适用于5.4及以上。所以,用它替换为阵列()

The first is because the new [] syntax for instantiating arrays only works in 5.4 and above. So, replace it with array():

// 5.4+ only:
array($userName => ['score' => $score]);
// 5.3 (and earlier) and 5.4+
array($userName => array('score' => $score));

二是不同的5.4功能,即访问阵列从函数返回,你应该使用一个临时变量:

The second is a different 5.4 feature, that of accessing arrays returned from functions, where you should use a temporary variable:

// 5.4+ only:
$this->Auth->user()['id']
// 5.3 (and earlier) and 5.4+:
$result = $this->Auth->user()
$result[id]

或者,preference,生产服务器升级到PHP版本比你正在使用的四或五年的旧版本更现代一点。为了节省更多的这些烦恼,你要么需要做的或开始在5.3本地开发。 (如果你需要做的是后者,我想看看你的虚拟化发展的设置,所以你可以在反对5.3为老年人生产系统的虚拟箱发展。)

Or, for preference, upgrade your production server to a PHP version that's a bit more modern than the four-or-five year old version you're using. To save on more of these headaches, you either need to do that or start developing locally in 5.3. (If you need to do the latter, I'd look into virtualising your development setup, so you could develop in a virtual box against 5.3 for older production systems.)

这篇关于PHP的方括号语法错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆