Short array syntax in PHP 5.6 doesn't work - php

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. ))

Related

Google Api PHP Client Syntax Error on PHP 7

OS: macOS Big Sur
PHP version: 7.4.1
Package name and version: google/apiclient: ^2.12.3
Whenever I switch the PHP version to 7.4.1 I get this error
Parse error: syntax error, unexpected 'static' (T_STATIC) in /Users/webtechstreet4/Local Sites/fvtest/app/public/wp-content/plugins/form-vibes-pro/vendor/psr/cache/src/CacheItemInterface.php on line 75
The library works great on PHP 8 but gives the above error when switching to PHP 7.
Screenshot
Actually, I sorted it out by changing the system PHP version and running composer update.
I thought I needed to change the site PHP version but it was actually the system PHP version.
here is the GitHub issue link: https://github.com/googleapis/google-api-php-client/issues/2246
it is because, static return type is available only for PHP 8, I have same issue, just for temporary you can change
.\vendor\psr\cache\src\CacheItemInterface.php
line number 75 just remove return type static like this
public function set($value);

Syntax error with session function on php5.3

The following function works on php 5.6 but gives error on php 5.3 which is the version of the cpanel im using. im unable to update the cpanel php version so i need to find a workaround..
$url =isset($_SESSION['url']) ? $_SESSION['url'] : [];
the error is:
Parse error: syntax error, unexpected '[' in /home/mydomain/public_html/mypage.php on line 7
any workaround is appreciated, thanks in advance.
Use array() instead of []. Yeah, it's PHP, not JavaScript.

Php square brackets syntax error [duplicate]

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

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