Anugrah
Anugrah

Reputation: 152

CakePHP application displays Syntax error, unexpected [

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! :)

Upvotes: 1

Views: 2135

Answers (2)

Nunser
Nunser

Reputation: 4522

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)

Upvotes: 3

elclanrs
elclanrs

Reputation: 94101

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']...

Upvotes: 0

Related Questions