This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 7 years ago.
Why My Script Not Work In Local USbwebserver
Parse error: syntax error, unexpected '[' in F:\Danyial\USBWebserver
v8.5\root\supportify\test.php on line 4
And Its Working On MY Server Side
<?php
//why its not work
if(mysql_fetch_array(mysql_query("select count(*) from gatwaylog"))[0]==0){
echo 'hi';
}
//and why it work
$data=mysql_fetch_array(mysql_query("select count(*) from gatwaylog"));
if($data[0]==0){
echo 'hi';
}
?>
Array dereferencing isn't supported in php version <= 5.3 (*)
As of PHP 5.4 it is possible to array dereference the result of a function or method call directly. Before it was only possible using a temporary variable.
As of PHP 5.5 it is possible to array dereference an array literal.
Related
This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 5 years ago.
I was running the following line on PHP5.4 without any problem:
$lstContent = $data->xpath("/response/lst[#name='highlighting']")[0]->lst[$i]->arr->str;
But now on PHP5.3 (production system) I get the following error:
Parse error: syntax error, unexpected '[' in /var/www/html/upload/inc_suche_code.php on line 153
Any ideas for a quick fix?
Updating PHP won't work for me.
In older versions of PHP you can not access array values directly on variables that are the result of a function. You have to split up the expression using a temporary variable.
$result = $data->xpath("/response/lst[#name='highlighting']");
$lstContent = $result[0]->lst[$i]->arr->str;
As of PHP 5.4 it is possible to array dereference the result of a function or method call directly. Before it was only possible using a temporary variable.
Source: http://php.net/manual/en/language.types.array.php
Edit: Obligatory "you should also consider upgrading your PHP version". This annoying limitation was fixed ages ago, not to mention that the 5.3 had its end of life in 2014, meaning it has not received security upgrades since.
This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 5 years ago.
hello in my PHP file i got error on host for this line that defines const array:
const telegram_methods=['sendMessage'=>'sendMessage','answerCallbackQuery'=>'answerCallbackQuery','forwardMessage'=>'forwardMessage'];
after some changes in host configurations. the error is:
Parse error: syntax error, unexpected '[' in /........./st_datas.php on line 8
what is the problem of this line or host configurations?
and this post:
PHP Parse/Syntax Errors; and How to solve them?
is so general and not duplicate of my question.
Maybe for your php version. If your php version >= 5.4, you can use brackets to define array.
So try it:
const telegram_methods = array('sendMessage'=>'sendMessage','answerCallbackQuery'=>'answerCallbackQuery','forwardMessage'=>'forwardMessage');
const telegram_methods=array('sendMessage'=>'sendMessage','answerCallbackQuery'=>'answerCallbackQuery','forwardMessage'=>'forwardMessage');
In regards to your follow up question (arrays are not allowed in class constants) it means simply as it says.
This has been updated in PHP 7 but before then if you need to set a class variable as an array simply drop the constant.
Check out the comments here for a more detailed response.
http://php.net/manual/en/language.oop5.constants.php
This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 6 years ago.
$router->addRoutes('', ['controller'=>'Home', 'action'=>'index']);
during compilation its generate an error
Parse error: syntax error, unexpected '['
Please help.
If is not >=5.4
PHP versions <= 5.4 do not support the [] syntax for array construction. Instead you shoud use array():
$router->addRoutes('',array('controller'=>'Home', 'action'=>'index'));
This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 7 years ago.
I tried to define an simple array in php, and I got an "unexpected '[' error message.
Code:
<?php
$days = ["Mon","Tue","Wed","Thu","Fri"];
echo "Value at index 1 is ". $days[1];
?>
while using the $days = array("Mon","Tue","Wed","Thu","Fri"); works fine
If your version of PHP is < 5.4 you cannot use [] to define an array. Create a page that contains:
<?php phpinfo() ?>
and see what version that says. Alternatively, if you have shell access to the server you're working on, typ this on the command line:
`php -v`
This question already has answers here:
PHP: Access Array Value on the Fly
(9 answers)
Closed 8 years ago.
Why does this work:
$n = explode("#", "some#email.com");
echo $n[0];
And this not work?
explode("#", "some#email.com")[0]
When I try the latter, I get:
Parse error: syntax error, unexpected '['
It works in later versions of PHP (>= 5.4.0):
PHP 5.4.0 offers a wide range of new features:
[...] - Function array dereferencing has been added, e.g. foo()[0]. [...]
Older versions of PHP do not support function array dereferencing, which is why you get a syntax error (PHP does not know what to do with the [, so it tells you it is "unexpected").