CakePHP application displays Syntax error, unexpected [

橙三吉。 提交于 2019-12-24 15:42:07

问题


This is the error I'm receiving:

Error: syntax error, unexpected '[' 
Line: 10

I'm running my cakephp app on a linux server ubuntu 3.7, it's cakephp 2.3.7 and PHP 5.3.1. Now, I'm running WAMP on EC2 after installing linux. On my localmachine I run XAMPP on Windows 7, and it does not get the same error. This is the code where it displays error:

 10:  <?php foreach ($this->Session->read('Customer')['Addresses'] as $key => $value) {
 11:  $ids[$z++] = $value['id'];
 12:  ?>
...

Since it does not give any error on localmachine, I'm assuming it's got something to do with the server environment. Please help, Thankyou! :)


回答1:


Problem is with your PHP version. PHP < 5.4 doesn't accept things as somefunction()['array'].

The solution would be to separate that function like

$customer = $this->Session->read('Customer');
foreach ($customer['Addresses'] as $key => $value) {
   //etc

The problem is documented and you can find another questions regarding that around.

(PD: of course, other solution is to upgrade PHP to 5.4 at least, but you'll need to keep in mind the migration changes)




回答2:


Only PHP 5.4+ supports "Function array dereferencing":

http://php.net/manual/en/migration54.new-features.php

You have to assign the result to a variable first to work on older versions:

$cust = $this->Session->read('Customer');
foreach ($cust['Addresses']...


来源:https://stackoverflow.com/questions/18623774/cakephp-application-displays-syntax-error-unexpected

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!