PHP unexpected '[' - php

I think it's a server configuration error and not a php syntax error.
running Apache/2.2.20 (Ubuntu) and PHP Version 5.3.10-1ubuntu3.7
Error :
PHP Parse error: syntax error, unexpected '[' in /home/thomas/Documents/myimouto/config/application.php on line 30
here is the code
$config->load_files = [
'app_functions.php',
'Moebooru/Resizer.php',
'dtext.php'
];
$config->safe_ips = [
'127.0.0.1',
'192.168.1.###'
];

Check your PHP version first. The short hand form for the array declaration [] was only introduced in PHP's version of 5.4 (if am not mistaken). The previous versions only supported the () delimiters.
So, I suggest you try changing that line to:
$config->safe_ips = array(
'127.0.0.1',
'192.168.1.###'
);
or better yet, update to PHP > 5.4Yeah..

Related

Is this PHP function definition wrong?

I've a moodle 3.4 installed on a server and cron fails.
There is a function definition that causes the error (PHP version is 7.0.x):
public static function create($time, int $courseid, int $categoryid = null) : calendar_information {// code here}
Is this a right syntax or maybe is this a bug? I have no good knowledge on PHP.
This is the error I am getting:
PHP Parse error: syntax error, unexpected ':', expecting ';' or '{' in /usr/home/xxx/www/calendar/lib.php on line 1047
Solved:
The crontab was pointing to the 5.6 version. So when I executed php -v from command line, the outpot was php 7.0.26. But cron process crashed because php was 5.6 version... Now it runs perfectly.
Thanks for your hints, all have been very useful.
Have a nice day!

Windows PHP-CLI truncates included scripts

I'm really baffled by an issue of running a small PHP application on Windows using the CLI (5.6).
I am simply running my app with:
bin\php -f sync.php
What I get is a syntax error issued out of an include'd file. The bizarre thing is, it appears that PHP is actually truncating my included script during processing. I know this because if I remove some of that scripts content (reducing total bytes), I can eventually get it to process without error.
Other relevant information:
Running PHP 5.6
Running on a Windows 7 VM
Application function is to connect to a SQLLocalDb instance
PHP sqlsrv driver is installed
Errors are thrown when included script exceeds ~633 bytes
I have tried to switch both encoding and line endings to remedy without success
If you think the error output is relevant, here you go:
PHP Warning: Unexpected character in input: ' in S:\config.php on line 27
Warning: Unexpected character in input: ' in S:\config.php on line 27
PHP Parse error: syntax error, unexpected end of file in S:\config.php on line
27
Parse error: syntax error, unexpected end of file in S:\config.php on line 27
If I eliminate some characters from that script while keeping valid syntax, it will execute without error. Any thoughts as to the cause?
Edit: It turns out that something may be wrong with the format of my included script. So I'm posting it for context. Maybe it's been that long since I've worked on Windows, that I am forgetting some bizarre rule about PHP syntax:
<?php
/**
* Configuration settings.
*
* PHP Version 5
*
* #category Configuration
* #author XXXXXX XXXXXXXX
* #copyright 2016 XXXXXX XXXXXXXX, XXX. All Rights Reserved.
*/
$config = array(
'magento' => array(
'wsdl' => 'http://redacted-url-still-filling-needed-size/api/v2_soap/?wsdl=1',
'username' => 'user',
'password' => 'pass',
),
'last_sync' => '2016-09-01 00:00:00',
'db' => array(
'adapter' => 'app_db_sqlsrv',
'host' => 'localdbt',
'name' => 'dbname',
'username' => 'root',
'password' => 'password',
),
);

Yii2 init error on PHP7

I've a problem when i try to switch my project from dev enviroment to prod, the project is a git repository I push to my server, now I need to switch to production and I try to runphp init on server but all time I receive the same error:
Parse error: syntax error, unexpected T_FUNCTION in /my/root/path/init on line 70
where /my/root/path/ is the base path where I push code.
Someone have any idea about this error?
Based on my previous answer and the comment:
Try
array_walk($skipFiles, function(&$value, $key, $data) {
$value = $data[1] . '/' . $value;
}, [$env, $root]);
#Marber: Return the same error, a similar solution is function resolveBug($value) { $value = "$root/$value"; } array_walk($skipFiles, resolveBug($value)); and this resolve the bug but the procedure generate anothe error on line 81: Parse error: syntax error, unexpected '['... and the code is $callbacks = ['setCookieValidationKey', 'setWritable', 'setExecutable', 'createSymlink'];
I'm guessing there is PHP 7 on the CGI side and CLI is PHP < 5.3 and that is why console commands don't work.
Check your console PHP version by running
php -v
in console.

PHP: syntax error, unexpected '[' when using array_map

I've been creating a php script for a project and have been running it on my development server which is running PHP 5.4.17
I need to move it over to my production server which is running PHP 5.4.19
When using the array_map function on my development server I got the results I needed with no problem.
On my production server I get the parse error:
Parse error: syntax error, unexpected '[' in /path/to/script/ on line 219
My code used was:
$arr = array_map(
function($results_titles, $results_image, $results_summary, $results_dates, $results_links) {
return ['title' => $results_titles, 'image' => $results_image, 'summary' => $results_summary, 'date' => $results_dates, 'link' => $results_links];
},
$results_titles, $results_image, $results_summary, $results_dates, $results_links
);
The new array syntax comes up with PHP 5.4.
So make sure your php version in your server is >= PHP 5.4
Note: the cli and the web server(eg: apache) could run different version of php.

php 5.3.24 - cli differs from http while using function()[i]

Why array indexing [i] won't work on functions while invoking the script from command line? (PHP version is on question in order to filter views.)
Test case:
<?php
echo explode( ' ', php_uname() )[0];
?>
Command line output:
> php -n -f borrar.php
Parse error: syntax error, unexpected '[', expecting ',' or ';' in borrar.php on line 3
Browser output:
Windows
EDIT
XAMPP php version was different from another PHP version that was previously installed in the system. That explains why I got different behaviours.

Categories