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.
Related
This question already has answers here:
Colon after method declaration?
(2 answers)
Closed 4 years ago.
It's almost impossible for me to search for an answer to this as I don't know what this syntax is called.
I have a server running a relatively old version of PHP (5.5.38) and I'm getting parse errors when I run a file that has the following:
public function foo(): array
{
...specifically the : array suffix.
Can anyone tell me what this syntax is called (so I can research further) and which PHP version introduced it?
This was added in PHP 7, they're called return type declarations.
http://php.net/manual/en/functions.returning-values.php#functions.returning-values.type-declaration
This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 4 years ago.
I don't know why the code below giving an error on my laptop while not at my friend's.
<?php
function myfunction() : int {
return 10;
}
echo myfunction();
?>
Error
Parse error: syntax error, unexpected ':', expecting '{' in (my location) on line 2.
If I remove the ": int" on line 2 everything is fine, but can someone explain why this code can't run on mine?
Please read the documentation. It is a PHP7+ only feature.
What might be a good idea as a work around, until you migrate to PHP7, is to do the following:
function myfunction() {
return (int)10;
}
var_dump(myfunction());
That will convert the return to an integer.
It's worth noting, this won't throw any warnings if the return value cannot be resolved.
I.E. If you passed parameters and those parameters were letters in a string, for instance, you'd get Warning: A non-numeric value encountered. However, for now, I think the above solution will suffice.
I strongly recommend upgrading to the latest version of PHP, though.
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 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.
This question already has answers here:
PHP Difference between array() and []
(5 answers)
Closed 8 years ago.
I have an Index php page that contains a code that prints the values of an array $a=['f','a','b'];.
The code works fine on WAMP server on my computer however when I upload the page online on a server like 000webhost an error comes up that says there is unexpected '[' on line of the array. Does anyone know why this happens?
Your PHP version doesn't support the [] notation, this kind of notation is available from PHP 5.4, use $a = array('f','a','b'); instead.