Search code examples
phpcodeignitersyntaxgetimagesize

Unexpected '[' when using getimagesize()


78I got some code, which used to work, but now passes an error:

Parse error: syntax error, unexpected '[' in (...)/utility_helper.php on line 47

I have checked, over and over again, that all parenthesis and similar are closed, and I cant find anything which looks incorrect. The function including line 47 is this:

/*  image_ratio($img)
 *  Returns one (1) if the image is landscape ratio (width > height) or reutrns 
 *  zero (0) otherwise 
 */
function image_ratio($img) {
    $imgWidth  = getimagesize($img)[0]; // <-- Line 47
    $imgHeight = getimagesize($img)[1];

    if ($imgWidth/$imgHeight > 1) {
        return 1;
    } else {
        return 0;
    }
}

What the heck am I doing wrong?

Update:

Changed link 47-48 to the following (old PHP version could not handle the above syntax):

$imgSize   = getimagesize($img);
$imgWidth  = $imgSize[0];
$imgHeight = $imgSize[1];

Solution

  • As said in the comments by Ben, PHP < 5.4 doesn't support array dereferencing from a function. You should do it like this or update your PHP version:

    function image_ratio($img) {
        $imgSize  = getimagesize($img); // <-- Line 47
    
        $imgWidth = $imgSize[0];
        $imgHeight = $imgSize[1];
    
        if (($imgWidth/$imgHeight) > 1) {
            return 1;
        } else {
            return 0;
        }
    }