PHP - cannot create array [duplicate] - php

This question already has an answer here:
Parse error: syntax error, unexpected '[' with php 5.3 [duplicate]
(1 answer)
Closed 8 years ago.
I downloaded and installed Ampps and now I'm using PHP version 5.3.28. When I try to create array i.e.
$foo = ['bar'];
or
$foo = [];
or
$data = [
'ts' => time(),
'ip' => $_SERVER['REMOTE_ADDR'],
'user_id' => #$auth->id,
'method' => $_SERVER['REQUEST_METHOD'],
'uri' => $_SERVER['SCRIPT_NAME'],
'data' => json_encode([
'get' => $_GET,
'post' => $post,
]),
];
etc. I always got error "Parse error: syntax error, unexpected '[' in ...". I didn't change anything. Where is problem and how can I fix it?

Using the syntax [] requires PHP 5.5.0 5.4 and higher, earlier versions have to define arrays as:
$array = array( /* data */);
So, if you wish to use the syntax as exampled. then plan an upgrade to 5.5, otherwise use the alternative method to define an array -- Array Documentation

Try something more explicit like this:
$dataTest = array
(
"ts" => time(),
"ip" => 7
);
echo $dataTest["ts"];
echo $dataTest["ip"];

Related

syntax error, unexpected identifier enum laravel

I am using LARAVEL 9. I am create enum in Enum folder and access in model. But when i am adding data i am getting this error
syntax error, unexpected identifier "GenderEnum"
Here is my code
GenderEnum.php
<?php
namespace App\Enum;
enum GenderEnum:string
{
case MALE = 'male';
case FEMALE = 'Female';
}
AdminSeeder.php
$data = [
'first_name' => 'Rishab',
'last_name' => 'goyal',
'email' => 'RISHABGOYAL#yopmail.com',
'mobile_number' => '123',
'role' => '1',
'gender' => 'male',
'password' => '123',
'profile_photo' => '',
];
Admin::addEdit($data);
Admin.php (Model)
protected $casts = [
'gender' => GenderEnum::class
];
There's nothing wrong with your code even in the namespace. The problem is your environment setup, maybe you are still running PHP 8.0 or below instead of PHP 8.1
Enums is a new syntax introduced in PHP 8.1, and not
supported in older PHP versions.
Parse error: syntax error, unexpected identifier
Please check 'use App\Enum' namespace is imported properly
I had the same problem before.
My solution is to update your PHP version to 8.1.

PHP return array | Parse error: syntax error, unexpected '[' in [duplicate]

This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 5 years ago.
I am currently building a response class with PHP to communicate with my backend API if everything went alright. This is the class:
public static function Success($data = null)
{
if (!$data) {
return [
'result' => true
];
} else {
return [
'result' => true,
'data' => $data
];
}
}
But when someone tries to install the plugin on a website it gives the error:
Parse error: syntax error, unexpected '[' in filename on line 13,
Line 13 is where the first bracket [ starts at the first return.
Does anyone know why I get this error? I think it has something to do with the version of PHP or Wordpress.
use
return array(
'result' => true
);

Laravel: Cannot use helper inside php shorthand array [duplicate]

This question already has answers here:
Is it possible to define a class property value dynamically in PHP?
(2 answers)
Closed 7 years ago.
PHP version 5.6. Code:
protected $siteServices = [
1 => [
'title' => 'Consulting',
'key' => 'service',
'description' => '',
'file' => asset('assets/img/sample/image1.jpg'), // throws error on this line
],
];
Error: PHP Parse error: syntax error, unexpected '(', expecting ']'
What would be a possible fix for this?
Edit
Solved by moving the variable in the running function instead of making it protected. Also can be solved by declaring the empty variable first then set the values in __constructor()
It could be done but it in a different way. I have listed two different methods.
First method
protected $siteServices = [
1 => [
'title' => 'Consulting',
'key' => 'service',
'description' => '',
'file' => ['asset', 'assets/img/sample/image1.jpg'] // throws error on this line
]
];
I replaced the function call with an array assigned to the file key in this array. So, now, you'd be wondering how you could call the asset function, yeah? It is pretty simple.
While you are looping through that array, you can do this:
call_user_func($siteServices[1]['file'][0], $siteServices[1]['file'][1]);
So, what are we doing here?
Firstly, we set an array to the file key where the first element of the array is the name of the function, while the other element is the value for the parameter that needs to be passed to the function defined previously.
So, using the PHP's call_user_func function, you can call functions giving their name and the parameters. I hope this helps you out.
Second method
I am pretty sure you will have a setter function for this property. Therefore, you could do something like this:
public function setSiteService($title, $key, $description, $file)
{
$file = asset($file);
$service = [
'title' => $title,
'key' => $key,
'description' => $description,
'file' => $file
];
$this->siteServices[] = $service;
}
So, the setter does the processing part for you. Hardcoding arrays is not at all a good idea, you should definitely populate through some mechanism.

PHP::MongoCollection->aggregate alternative or fix

I'm using xhgui and getting this error:
127.0.0.1:27017: no such cmd: aggregate
Stack trace:
/var/www/profiler/src/Xhgui/Profiles.php(172): MongoCollection->aggregate(Array)
Code:
$mongo = new MongoClient($config['db.host'], $config['db.options']);
$db = $mongo->$config['db.db'];
$results = $db->results->aggregate(array(
array('$match' => $match),
array(
'$project' => array(
'date' => $col,
'profile.main()' => 1
)
)
));
Is it possible to fix this error? Maybe need to update mongo extension OR is there any other alternative way?
This appears to actually be a problem with your MongoDB instance itself. Aggregation was introduced in version 2.2 of MongoDB, so if your server is running an older version, it won't understand the aggregate() command.
https://docs.mongodb.org/manual/core/aggregation-introduction/

Unexpected start of array [ [duplicate]

This question already has answers here:
Unexpected bracket '[' - PHP [duplicate]
(3 answers)
PHP 5.4 vs 5.3 app errors
(3 answers)
Closed 9 years ago.
I keep getting this error
syntax error, unexpected '['
it is in the line 1 of the following code snippet:
Route.php
Route::get('api/test/{input}', [
'before' => 'checkauth',
"as" => "test",
"uses" => "TestController#show"
]);
what is wrong?
Change your array notation.
Route::get('api/test/{input}', array(
'before' => 'checkauth',
"as" => "test",
"uses" => "TestController#show"
));
As Michael Berkowski says, the use of the array notation with [ requires PHP 5.4+
From the Php array documentation
As of PHP 5.4 you can also use the short array syntax, which replaces array() with [].
You should use the array(); notation:
Route::get('api/test/{input}', array(
'before' => 'checkauth',
"as" => "test",
"uses" => "TestController#show"
));

Categories