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

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.

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!

Error 500 on a working docker container

I'm using a slimframwork 3 app on laradock (nginx, mariadb, phpfpm, php 5.6) so I made a stupid syntax error:
$view->addExtension(new \Slim\Views\TwigExtension(
$container->router,
$container->request->getUri(),
));
the comma after the getUri() was giving me error 500 on chrome and that was frustration so I tried my app on wamp on windows and I get:
Parse error: syntax error, unexpected ')' in C:\wamp64\www\app\bootstrap\app.php on line 21
why was I having a 500 error with no clue of what was wrong.
P.S. I've set displayErrorDetails to true
Shoutout to Phil who solved this issue.
So when you use laradock and you come to a situation like this and you want more specific details about the error, you can either check your php-fpm container logs with:
docker logs -f <your container>
or set the "display_errors=On" on laradock/php-fpm/laravel.ini
and you'll see your error message on your browser instead of error 500.

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 unexpected '['

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

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