Error parsing short array syntax when using json_encode() - php

I am sharing my work with some team. On my side, this line json_encode(['Succeeded']) works perfectly, on the team side this fails with this message
Parse error: syntax error, unexpected '[', expecting ')' in /home/app/file.php on line *
Why is this so ?

I think you might have two different versions of PHP installed.
Short array syntax was introduced in version 5.4.0. See http://php.net/manual/en/migration54.new-features.php
Therefore, the following will probably work on both:
json_encode(array('Succeeded'));

That syntax is supported only as of php 5.4.0:
http://php.net/releases/5_4_0.php

Related

Laravel parse error: syntax error, unexpected T_CLASS, expecting T_STRING

I developed a laravel application back in August this year, and it was working fine then. I am trying to run that application now, and it returns this error:
parse error: syntax error, unexpected T_CLASS, expecting T_STRING or
T_VARIABLE or '{' or '$' in D:\bkonme\artisan line 31
And line 31 is like this:
$kernel = $app->make(Illuminate\Contracts\Console\Kernel::class);
My PHP version is 5.6.14 and I am using XAMPP on windows platform. I have some idea of it happening because of some version conflict between laravel and PHP, but i don't know how to resolve that issue, any help?
Even if you have PHP/5.6.14 installed, your app is definitively not using it. You could not use class as identifier until PHP/5.5 (demo).
The feature is called Class name resolution via ::class and it's described in the Migrating from PHP 5.4.x to PHP 5.5.x chapter of the PHP manual.

Sudden PHP parse error on RHEL

I have various PHP files, none of which have been modified prior to the app working and it breaking. My problem is that for some reason, these files are suddenly throwing PHP parse errors for things like 'unexpected' square brackets in seemingly random places throughout the file, but upon manually checking the file, the syntax is correct.
The only thing I did that could have 'altered' the files would be various chmod operations to change the permissions. But how/why would this change the actual content of the files?
The files are called on boot just as they always have been, and have worked fine in the past, up until the recent chmod changes.
Is this permanent, or will I just have to restore my system from an earlier time?
RHEL 6.5, PHP 5.4.
Edit:
Example of the error:
PHP Parse error: syntax error, unexpected '[' in /var/ptc/CRM-dev/tools/init on line 122
What's on line 122:
$files = [];
Another example:
PHP Parse error: syntax error, unexpected '[', expecting ')' in /var/ptc/CRM-dev/vendor/react/promise/src/functions.php on line 68
What's on line 68:
return resolve([]);
But these files themselves haven't been changed (as far as I'm aware), and the syntax looks correct.
phpinfo() gives 5.4.39, which is the same as when running php -v on the server.
If the PHP version has been changed then it may be stumbling on the PHP 5.4 square bracket array syntax.
Use phpinfo(); to be sure that the PHP version running the web server is the same as the one you're using on the CLI.

php Parse error: syntax error, unexpected '['

When I run this script on my local server it works just fine. However when I run it on a hostgator server I get this error:
Parse error: syntax error, unexpected '[' in /home/user/public_html/parsing/parse.php on line 46
Line 46 is this:
$rows = [];
Any idea why this could be happening?
Thank you.
By the way I have only copied up to line 46, there are a few more lines of code than shown here.
The new array syntax [] is only available in versions 5.4 and greater.
If you are using a PHP version prior to 5.4 you have to use this syntax to create an empty array
$row = array();

Getting a syntax error unexpected T_STRING for namespace line

Well, I keep getting
Parse error: syntax error, unexpected T_STRING in /path/to/index.php on line 2
Googled my arse off and still nuthing, so far the index.php contains:
<?php
namespace Infire; # Line 2
...
I am using PHP 5.1
Any ideas?
The PHP namespace is only supported in PHP 5.3+ version
Check this PHP 5.3.0 Release Announcement
I've found namespaces introduced in PHP 5.3 to suffer from multiple usability problems due to performance trade-offs, limitations of PHP's parser, collisions with other PHP features and unfortunate design decisions.

PHP adding value to array

In PHP why is this a syntax error:
$row['some_key'] = "kkkk";
Raises
Parse error: syntax error, unexpected T_STRING
From the documentation I understood this to be perfectly valid PHP, yet it throws the same error every time (and the error of course goes away then I comment the line out).
Edit: After long searching I actually noticed that the file is indeed corrupted in some way. In Vim (on ubuntu 12.04) the file looks just normal. But when viewing from other computers it's corrupted. So not related to PHP.
In PHP why is this a syntax error:
It's not. If you're getting unexpected T_STRING, the problem is elsewhere (hint: look at the line above).

Categories