2

I'm having a strange error which I haven't been successfully able to google a result for or find an explanation i can understand on stackOverflow. My code works fine on my local server, performing as I expect, but when I upload it, it returns the following error '*syntax error, unexpected '['in /home/maxster/max-o-matic.com/_inc/functions.php on line 59*'.

Below is line #59:

$aaPlaceholderIndex = array_keys($aaPlaceholder)[$row*4+$image]; //if associative array

Here is the whole function:

function PrintFolio($aaPlaceholder)

{
//print out X number rows, four items at time

$itemsCount = sizeof($aaPlaceholder);//get size of array
$height = (int)ceil(sizeof($aaPlaceholder)/4);//get size of array divided by 4

//loop through array of X items, for each group of 4 print as a row
for($row = 0; $row < $height; $row++) //looping through the rows

    {
        echo '<div class="row flush">'; //open div
        for($image = 0; $image < 4; $image++)    //each row is composed of 4 images
        {
             //$aaPlaceholderIndex = $row*4+$image; //if indexed array
             $aaPlaceholderIndex = array_keys($aaPlaceholder)[$row*4+$image]; //if associative array
             if( $aaPlaceholderIndex <= $itemsCount ) {
                 printf(TEMPLATE_PORTFOLIO, $aaPlaceholderIndex, $aaPlaceholder[$aaPlaceholderIndex]);
                 //$template = '<div class="3u"><a href="_img/fulls/%1$s" class="image full"><img src="_img/thumbs/%1$s" alt="" title="%2$s" /></a></div>';
            }
        }

        echo '</div>'; //end div group of 4
    }//end loop
}//end function

I'm scratching my head on this one a lot as it seems like the function is fine, it's format seems proper to me and everything is in a logical place with a good flow. I'm sure it could be better, but for me this is really good given my skill level - i'm a newb. Any thoughts on what be causing the issue with the Square brace?

2 Answers 2

5

Array Dereferencing was not introduced until PHP 5.4. Your production version is not running PHP 5.4 but your local environment is. So:

$aaPlaceholderIndex = array_keys($aaPlaceholder)[$row*4+$image]; //if associative array

becomes:

$tempArray = array_keys($aaPlaceholder); //if associative array
$aaPlaceholderIndex = $tempArray[$row*4+$image]
Sign up to request clarification or add additional context in comments.

1 Comment

So it's that my host environment is running an earlier version of php. WOW, the things you learn by asking. Thank you I will explore this and look into upgrading my server. Thank you.
0

Quick note following on from John Conde's answer in case it's useful for anyone else - I had the same issue which was fixed by upgrading PHP, which I did by adding the following to .htaccess

AddType x-httpd-php55 .php

I don't know if this will work on all servers, but it was a quick and easy fix for me :)

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.