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

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.

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

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.

Parse error building website with CakePHP

I recently did the blog tutorial for CakePHP, as is found here: http://book.cakephp.org/2.0/en/tutorials-and-examples/blog/blog.html
I have CakePHP version 2.8.5, and WampServer with Apache 2.4.9, PHP 5.5.12 and MySQL 5.6.17, and got the blog running on localhost/.
I am now trying to upload the blog to a server online, and have used SmartFTP to upload files to the folder public_html on a free web server online, but when I visit the domain, I receive:
Parse error: syntax error, unexpected T_STATIC, expecting T_STRING or T_VARIABLE or '$' in /home/a3087838/public_html/lib/Cake/Core/App.php on line 221
Line 221 is:
if (!empty(static::$legacy[$type])) {
which is part of:
public static function path($type, $plugin = null) {
if (!empty(static::$legacy[$type])) {
$type = static::$legacy[$type];
}
I have seen similar questions asked online, but the solution seems to be that PHP version 5.3 or greater is required as prior versions don't support late static binding, but my version is 5.5.12, so I shouldn't have that problem.
I tried replacing the word static with self, but the error then just reiterates itself for later lines containing static. I repeated this down to line 282 and it's behaviour didn't change. I could replace all other instances of static with self, but that will take a while and I expect there is a better solution that I am missing, if that is even a solution. I am inexperienced and any help would be appreciated.

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

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