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 8 years ago.
I am having trouble with my elgg installation for my website. Every time I load the "install.php" in the root directory, I keep getting this error:
Parse error: syntax error, unexpected '[' in (Root)/engine/classes/Elgg/Di/ServiceProvider.php on line 194
Here is line 194 in that file:
$params = $c->config->get('cookies')['session'];
I have PHP version 5.3.13. Can anyone help?
Arrays can't be referenced directly from a return object in PHP below 5.4.
[Documentation]
You can't do this:
$params = $c->config->get('cookies')['session'];
But you CAN do this:
$cookies = $c->config->get('cookies');
$params = $cookies['session'];
Or you can just upgrade your PHP installation.
You can't do this because you don't have PHP version 5.4 or higher!
So change this:
$params = $c->config->get('cookies')['session'];
to this:
$params = $c->config->get('cookies');
$params = $params['session'];
For more information about array dereferencing see the manual: http://php.net/manual/en/language.types.array.php
And a quote from there:
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.
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.
This question already has answers here:
PHP syntax for dereferencing function result
(22 answers)
Closed 7 years ago.
I have a strange syntax problem in php.
At some point in my code, I do a mysql query (which only send back one line with one value), and I get the query result the following way :
$myvar = mysql_fetch_assoc($result)["something"];
$result being the mysql result, of course.
It works just fine, both locally and on the site.
However, when my colleague took the latest version of the site for some local test of his own, he got the following error :
Parse Error: syntax error, unexpected '[' in C:\wamp\www\mySite\ref\myFile.php on line 33
Line 33 being the line where I defined $myvar.
He fixed it by doing the following :
$myvar = mysql_fetch_assoc($result);
$myvar = $manif["something"];
It seems pretty obvious to me that the problem comes from wamp (I am personally running an apache server), so my question is "why?".
I only got into web development recently (I was more of a C++ developer until now) and I've been using this syntax for a while. Is it bad practice?
And why does wamp return an error but not apache? Do each server have their own php parser?
Thank you.
Function array dereferencing (foo()[0] for example) has been introduced in PHP5.4. Check your php version.
Since PHP 5.4 it's possible to do what you want. If you colleague use older version of PHP, that's why it's not working.
If you are using PHP prior to 5.3, you should use temporary variable, e.g. something like this:
$r = mysql_fetch_assoc($result);
$myvar = $r['something'];
This question already has answers here:
Reference - What does this error mean in PHP?
(38 answers)
Closed 8 years ago.
I have been developing locally using Mamp, and everything was great until I uploaded to the server. I have narrowed my problem down to having to do with the php version. Mamp was running on a newer version than the server.
If I run Mamp on PHP 5.6.2(or 5.5.X) I have no problems with my code. But if all I do is change the PHP version in Mamp preferences to PHP 5.3.29 if complains about the following line of code:
$shipping = reset($arrShipOptions['options'])[0]['price'];
The error is:
syntax error, unexpected '['
First thing that came to mind was that reset() might be a new function. But according to http://php.net/manual/en/function.reset.php it was already available in PHP 4
Could an extra pair of eyes shed some light on this please.
Thanks
In older PHP versions you have to assign result from reset (or any other function) to variable and then access it using [].
$shipping = reset($arrShipOptions['options']);
$shipping = $shipping[0]['price'];
before php 5.4 you cant chain syntax like that...
http://docs.php.net/manual/en/language.types.array.php
It's called array dereferencing. It is not available in php 5.3
// on PHP 5.4
$secondElement = getArray()[1];
// previously
$tmp = getArray();
$secondElement = $tmp[1];
The problem is caused by using a feature available form PHP 5.4+ called
Function array dereferencing
Source http://php.net/manual/en/migration54.new-features.php (the third feature)
The solution is breaking the code into two lines:
$shipping = reset($arrShipOptions['options']);
$shipping = $shipping[0]['price'];
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:
PHP: Access Array Value on the Fly
(9 answers)
Closed 8 years ago.
Why does this work:
$n = explode("#", "some#email.com");
echo $n[0];
And this not work?
explode("#", "some#email.com")[0]
When I try the latter, I get:
Parse error: syntax error, unexpected '['
It works in later versions of PHP (>= 5.4.0):
PHP 5.4.0 offers a wide range of new features:
[...] - Function array dereferencing has been added, e.g. foo()[0]. [...]
Older versions of PHP do not support function array dereferencing, which is why you get a syntax error (PHP does not know what to do with the [, so it tells you it is "unexpected").