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"
));
Related
I'm new to Blade, and couldn't find how to do that.
Unfortunately, Blade's documentation doesn't help much, but I've already tried the following:
#include('module.my-component', [
'param' => [{
'x' => 'A',
'y' => false
}, {
'x' => 'B',
'y' => true
}]
])
As expected, I've got the following syntax error:
Parse error: syntax error, unexpected '{', expecting ']'
Does anybody know the correct syntax?
You have to use valid PHP syntax. This is not valid PHP:
[{'b' => 'a'}]
This is valid PHP though:
[
(object) ['x' => 'B', 'y' => true],
(object) [ ... ],
]
That would be an array of stdClass objects. When you cast an array to object you get an stdClass object with those properties.
Basically everything inside a Blade directive is a single 'string'/'expression' ... when blade compiles it replaces itself with PHP and that expression usually just gets dropped in directly, so it is all PHP in the end, so it has to be valid. You can open up any compiled view from the storage folder to see this in action.
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
);
This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 6 years ago.
I am getting the following error and I just cant find the solution to the problem. Might be anyone be able to help me?
DB::table('videos')->insert(
['video_id' => $videos[$i]->title],
['url'] => $videos[$i]->url],
['default_thumb'] => $videos[$i]->default_thumb],
['thumb'] => $videos[$i]->thumb],
['publish_date'] => $videos[$i]->publish_date],
['tags'] => $videos[$i]->tags]
);
The error message is:
FatalErrorException in VideoController.php line 33:
syntax error, unexpected '=>' (T_DOUBLE_ARROW)
This is correct syntax for insert() method:
DB::table('videos')->insert([
'video_id' => $videos[$i]->title,
'url' => $videos[$i]->url,
'default_thumb' => $videos[$i]->default_thumb,
'thumb' => $videos[$i]->thumb,
'publish_date' => $videos[$i]->publish_date,
'tags' => $videos[$i]->tags
]);
Hope it will solve your problem
DB::table('videos')->insert(
['video_id' => $videos[$i]->title,
'url' => $videos[$i]->url,
'default_thumb' => $videos[$i]->default_thumb,
'thumb' => $videos[$i]->thumb,
'publish_date' => $videos[$i]->publish_date,
'tags' => $videos[$i]->tags]
);
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.
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"];