php cli: unexpected T_USE - php

I've got the following code which I want to run from CLI:
error_reporting(E_ALL);
ini_set('display_errors', '1');
require '/usr/share/nginx/www/aws-autoloader.php';
use Aws\Ec2\Ec2Client;
$ec2Client = Ec2Client::factory(array(
'key' => 'xxxxxxxxx',
'secret' => 'xxxxxxx',
'region' => 'us-east-1'
));
Am getting this error:
Parse error: syntax error, unexpected T_USE on line 5
The line that is causing the error is:
use Aws\Ec2\Ec2Client;
Is it not possible to import like this when using CLI?
Edit
PHP version:
PHP 5.3.6-13ubuntu3.9 with Suhosin-Patch (cli) (built: Sep 12 2012
19:00:27)

use statements should appear at the top of code - directly after the PHP tag open (or a namespace definition), or as the first lines in a class.
For an example of such a bug (solved) see https://github.com/browscap/browscap-php/issues/1#issuecomment-22737388

Related

I have a parse error using eloquent with slim3

I'm following the codecourse video series Authentication with Slim 3, which is about 3 years old.
I've hit a snag when it comes to using eloquent to access a mysql database.
(the relevant video: https://www.youtube.com/watch?v=70IkLMkPyPs )
the code wasn't quite the same as that currently recommended on the instructions on illuminate/database's docs, so I've modified the code slightly to follow that standard, but regardless of whether I follow Alex's code oon the vid or the following, a parse error results.
$app = new \Slim\App([
'settings' => [
'displayErrorDetails' => true,
'db' => [
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'opium3',
'username' => 'root',
'password' => 'root',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
]
],
]);
$container = $app->getContainer();
/*set up eloquent to use outside laravel*/
use \Illuminate\Database\Capsule\Manager as Capsule;
$capsule = new Capsule;
$capsule->addConnection($container['settings']['db']);
$capsule->setAsGlobal();
$capsule->bootEloquent();
Running the app produced this error:
Parse error: syntax error, unexpected '=' in /var/www/public/opium3/vendor/illuminate/database/DatabaseManager.php on line 65
I'm running this locally on a scotchbox vagrant set up, if that's relevant.
There's a similar question at
Parse error when run illuminate/database/capsule with bootEloquent() in PHP
but this answer doesn't resolve my issue, since the server is running php7.
I have also tried installing packages recommended by illuminate/database, installing php-mbstring, and editing the php.ini file, on the server, but the error persists.
Am I missing something obvious?
Make sure you're using at least PHP 7.1.
/illuminate/database/DatabaseManager.php is using a short hand version of the list()-construct which wasn't available until PHP 7.1.
The "old" way is to:
list($var1, $var2) = someFunction();
while you, since PHP 7.1, can also do:
[$var1, $var2] = someFunction();
The row the error is thrown in is this:
[$database, $type] = $this->parseConnectionName($name);
Note:
If you can't upgrade your PHP version from 7.0 to 7.1.3+, you need to use an older version of the Illuminate/Database-package. Use version 5.5 for PHP 7.0 and then 5.6+ on PHP 7.1.3+

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

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

MongoCollection::aggregate() is undefined in Heroku PHP using MongoHQ

I'm getting the following error when using MongoDB's aggregate() function in a PHP code. This code perfectly works on my local setup which is running MongoDB 2.2.3
PHP Fatal error: Call to undefined method MongoCollection::aggregate() in /app/www/page.php on line 52, referer: http://referrer.url
Code
foreach($cats as $key=>$val){
$cats2[$val['lable']] = $myCollection->aggregate( array(
array('$match' => array('user_id' => $user_id )),
array('$unwind' =>"\$data"),
array('$match' => array('data.category'=> $val['category'])),
array('$project' => array('name'=> "\$data.name", 'id'=>"\$data.id")),
array('$group' => array('_id'=>'$id', 'name'=> array('$first' =>'$name'))),
array('$limit' => 12)
));
}
Environment
PHP on Heroku
MongoDB 2.2.4 with MongoHQ Add-on
You have to look into your phpinfo()
If in the version you see something less than 1.3.0, then it will not work.
Go to mongo driver page download and add appropriate driver.
Restart the server and it will work.

Categories