This question already has an answer here:
Difference in accessing arrays in PHP 5.3 and 5.4 or some configuration mismatch?
(1 answer)
Closed 7 years ago.
I got few lines producing error 500. The error log says:
PHP Parse error: syntax error, unexpected '[' in...
and the lines are:
$catids[] = $params->get('catid');
and after commenting it out, this one comes up:
$return_category[] = self::_getCategoryInfor($tg, $params)[0];
How can i modify them in order to match with new PHP versions and solve the error 500?
From your code, I do not see code that may triggered this parse error. Try change your code so it becomes:
$categories = self::_getCategoryInfor($tg, $params);
$return_category[] = $categories[0];
This is the expression causing the error:
self::_getCategoryInfor($tg, $params)[0];
Function array dereferencing was added in PHP 5.4. Your code will generate a parse error on 5.3 or lower:
https://secure.php.net/manual/en/migration54.new-features.php
Related
This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 5 years ago.
I was running the following line on PHP5.4 without any problem:
$lstContent = $data->xpath("/response/lst[#name='highlighting']")[0]->lst[$i]->arr->str;
But now on PHP5.3 (production system) I get the following error:
Parse error: syntax error, unexpected '[' in /var/www/html/upload/inc_suche_code.php on line 153
Any ideas for a quick fix?
Updating PHP won't work for me.
In older versions of PHP you can not access array values directly on variables that are the result of a function. You have to split up the expression using a temporary variable.
$result = $data->xpath("/response/lst[#name='highlighting']");
$lstContent = $result[0]->lst[$i]->arr->str;
As of PHP 5.4 it is possible to array dereference the result of a function or method call directly. Before it was only possible using a temporary variable.
Source: http://php.net/manual/en/language.types.array.php
Edit: Obligatory "you should also consider upgrading your PHP version". This annoying limitation was fixed ages ago, not to mention that the 5.3 had its end of life in 2014, meaning it has not received security upgrades since.
I am currently trying to use the https://github.com/muesli/huephp to control my lights through PHP but I am running into a syntax problem.
Here is the line in huecli.php that I am getting a syntax error on where [...
Here is the actual error: "Parse error: syntax error, unexpected '[' "
$hue->lights()[$light]->setLight( $command );
There are more errors but it seems the [$light] is causing the problem. I haven't worked with this kind of syntax before so any help is appreciated!
You are probably not running PHP 5.4+ which is what is required to use Array Dereferencing. To use this code it must be modified as so:
$hue_lights = $hue->lights();
$hue_lights[$light]->setLight( $command );
This question already has an answer here:
PHP unexpected '['
(1 answer)
Closed 8 years ago.
I can not use a newer PHP version then 5.3 and i get this error message:
**Parse error: syntax error, unexpected '[' in PATH/product.php on line 17**
I founded on this page that is php version problem, how can i solve this in php 5.3?
The code on this line is exactly:
$_productids = [];
Old version doesn't support this syntax. Update this to:
$_productids = array();
Use old array syntax:
$_productids = array();
This question already has answers here:
Unexpected bracket '[' - PHP [duplicate]
(3 answers)
Closed 9 years ago.
I 'm try to commit this code to my server from my Ubuntu server
$me = $this->fetchAll(array('id'=>$Id,'ce'=>$e), array('cr'=>array('$slice' =>[$offset, $limit])));
but it return
PHP Parse error: syntax error, unexpected '[' in - on line 114
MSG: Failed to checkin branches/myFile.php, PHP said
what is the wrong with '$slice' =>[$offset, $limit] in order to commit it to my server
The []-short-array notation was introduced with PHP 5.4. Some *ix distros have still not introduced this new version to their default repos. Make sure your server uses php5.4 to parse that specific file. For the time beeing I'd recommed to use the 'old' array notation.
http://www.php.net/manual/en/migration54.new-features.php
You can't do it like that. Try this:
'$slice' => $offset . ', ' . $limit
I think that's what you're looking for.
This question already has answers here:
PHP syntax for dereferencing function result
(22 answers)
Closed 9 years ago.
My script is working really fine on my xampp. Now I tried to upload it on the server, but it spat directly a
Parse error: syntax error, unexpected '['
in my face. :(
The line which its mocking about is this one:
$item = $xml->xpath($path)[0];
And I have no idea what is wrong. I tried to look on the php 5.3 changelog but did not found anything about it. (Because I have 5.3 on the server, and on xampp its an olderversion)
The whole code block looks like this:
$path = '//item[#id="'.$id.'"]';
if ($xml->xpath($path)) {
$item = $xml->xpath($path)[0];
} else {
die('<p class="error">Script Error: Code 101 - Please contact administrator</p>');
}
I am thankful for any help, I cannot seach [ with google and have no idea where it could come from, since on xampp its working fine
Try this
$item = $xml->xpath($path);
$item = $item[0];