1

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

2 Answers 2

3

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)

Sign up to request clarification or add additional context in comments.

1 Comment

At my shared server I needed to update from PHP 5.4 to 5.5 fortunately it had an option in cpanel
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.