Php square brackets syntax error [duplicate] - php

This question already has answers here:
PHP 5.4 vs 5.3 app errors
(3 answers)
Closed 8 years ago.
on my localhost [PHP Version 5.5.9-1ubuntu4.5] this code is working:
array($userName => ['score' => $score]);
and also this code is working:
$this->Auth->user()['id']
but on production server [PHP Version 5.3.3-7+squeeze23] in both cases I've got an error:
Error: Fatal Error (4): syntax error, unexpected '['
what's going on? how can I fix it in a simplest way? (cause changing all arrays in project is highly impracticable and I'm even not sure how to manage the second case with Auth...)

The first is because the new [] syntax for instantiating arrays only works in 5.4 and above. So, replace it with array():
// 5.4+ only:
array($userName => ['score' => $score]);
// 5.3 (and earlier) and 5.4+
array($userName => array('score' => $score));
The second is a different 5.4 feature, that of accessing arrays returned from functions, where you should use a temporary variable:
// 5.4+ only:
$this->Auth->user()['id']
// 5.3 (and earlier) and 5.4+:
$result = $this->Auth->user()
$result[id]
Or, for preference, upgrade your production server to a PHP version that's a bit more modern than the four-or-five year old version you're using. To save on more of these headaches, you either need to do that or start developing locally in 5.3. (If you need to do the latter, I'd look into virtualising your development setup, so you could develop in a virtual box against 5.3 for older production systems.)

That syntax isn't supported until php version 5.4
You can see that here:
http://php.net/manual/en/language.types.array.php

The squarebracket Syntax for Arrays was introduced in php v. 5.4. Same goes for the usage of returned values of a function or method

Related

Code difference between PHP 5.3.10 and PHP 5.3.29 [duplicate]

This question already has answers here:
Reference - What does this error mean in PHP?
(38 answers)
Closed 5 years ago.
I need to make an HTTP call from a PHP script. I've tested it on my personal domain (running PHP 5.3.29) and it's all ok. When I've moved it on my customer domain (running PHP 5.3.10) the script starts having some problem.
In particular, this is the code that generate the error:
function BuildPlayFabHttpHeaders($i_PlayFabSecretKey) {
$headers = [
"Content-Type: application/json",
"X-SecretKey: $i_PlayFabSecretKey"
];
return $headers;
}
I think the problem is with that kind of declaration, but I'm not a php expert. Can anyone help me to get this running on PHP 5.3.10?
This wouldn't have worked on PHP 5.3.29 since the short array syntax [..] was introduced in PHP 5.4.
For anything under 5.4, you must use:
array(
key => value,
key2 => value2,
key3 => value3,
...
)
My assumption is your tests weren't actually using the PHP 5.3.29 binary but some other version installed on the system.

Short array syntax in PHP 5.6 doesn't work

What could be the reason this code doesn't work in PHP 5.6.23?
$change_text = [
"ok" => "Użytkownik zmieniony poprawnie.",
"new" => "Użytkownik dodany poprawnie.",
"delete" => "Użytkownik został usunięty.",
];
Works on my server with 5.6 and I've tested it on some other servers and it's fine. I moved a site to a new server and it has PHP 5.6.23 but this does't work...
Also the PHP documentation says:
As of PHP 5.4 you can also use the short array syntax, which replaces array() with [].
The error i get:
Parse error: syntax error, unexpected '[' in /...
Any ideas?
Sure the problem is old version of php. I had the same problem. I ran simple script with phpinfo() from browser and saw, that version is 5.6, but problem was in cli. So I checked version in cli, and It was 5.3! Thats it. ))

Unexpected [ - works locally, but not on shared hosting [duplicate]

This question already has answers here:
PHP unexpected '['
(1 answer)
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 6 years ago.
I'm using Slim Framework, and have set-up a very simple API to retrieve data from a database. This works flawlessly on my local machine, OS X El Capitan.
But - when moving the files to a shared hosting environment, all the routes throw 500 errors, with this error specifically:
Parse error: syntax error, unexpected '['
vendor/nikic/fast-route/src/functions.php on line 12
Line 12 is:
function simpleDispatcher(callable $routeDefinitionCallback, array $options = []) {
I can't work out why it is throwing a parse error on this line, and why it works on my local but not on shared, both running a version of PHP 5.5. Does anybody have any ideas on why this could be?
You are using an old PHP version in your server which doesn't properly support Slim router. You should upgrade to PHP 5.5 according to the minimum requirements of slim framework. [] style arrays do not work in older versions of PHP, hence the error message.
You need to replace the array declaration with the older style like this:
function simpleDispatcher(callable $routeDefinitionCallback, array $options = array()) {
And on all other occurrences in your code. Or if possible, just upgrade the PHP version that'll eventually support square brackets as array declaration syntax.

What i need to activate in PHP? to accept [

I am programming an app locally, but when I migrate to server I get a parse error using this line:
if(!is_array($data[array_keys($data)[0]]))
Returns:
Parse error: syntax error, unexpected '[', expecting ']' in
/home/file.php
If I rewrite the line like this:
$var1 = array_keys($data);
if(!is_array($data[$varX[0]]))
It works.
What do I need to activate on server, to get it working in the first example without error?
Thanks.
You need to be running PHP version 5.4 to use array dereferencing like that. I suspect on your server you have a lower version of PHP than on your local system.
http://php.net/manual/en/migration54.new-features.php
Under 'New features':
Function array dereferencing has been added, e.g. foo()[0].
What you're trying to do is called "array dereferencing" which only became available in PHP 5.4. So if your version of PHP is earlier than that like yours is, it won't work.

Laravel 4 syntax error out-of-the-box

I just installed Laravel 4 (Illuminate) and as I opened the index.php file in a browser, I was met with this error:
Parse error: syntax error, unexpected 'yield' (T_YIELD), expecting identifier (T_STRING) in /www/Laravel4/vendor/illuminate/view/src/Illuminate/View/Environment.php on line 339
I have fixed the permissions for the meta folder, and installed all the dependencies through Composer. I am running PHP version 5.5.0alpha2 on OSX 10.8.2.
That's because yield became a language construct in PHP 5.5 (used in Generators) - but someone decided that it's a good idea to use this short word to name a function:
public function yield($section)
{
return isset($this->sections[$section]) ? $this->sections[$section] : '';
}
Downgrade to PHP 5.4 (after all, it's the current mainstream version, 5.5 is not even in beta yet) and it should work fine.

Categories