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").
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 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 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 an answer here:
Difference in accessing arrays in PHP 5.3 and 5.4 or some configuration mismatch?
(1 answer)
Closed 8 years ago.
I am having trouble with my elgg installation for my website. Every time I load the "install.php" in the root directory, I keep getting this error:
Parse error: syntax error, unexpected '[' in (Root)/engine/classes/Elgg/Di/ServiceProvider.php on line 194
Here is line 194 in that file:
$params = $c->config->get('cookies')['session'];
I have PHP version 5.3.13. Can anyone help?
Arrays can't be referenced directly from a return object in PHP below 5.4.
[Documentation]
You can't do this:
$params = $c->config->get('cookies')['session'];
But you CAN do this:
$cookies = $c->config->get('cookies');
$params = $cookies['session'];
Or you can just upgrade your PHP installation.
You can't do this because you don't have PHP version 5.4 or higher!
So change this:
$params = $c->config->get('cookies')['session'];
to this:
$params = $c->config->get('cookies');
$params = $params['session'];
For more information about array dereferencing see the manual: http://php.net/manual/en/language.types.array.php
And a quote from there:
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.
This question already has answers here:
PHP syntax for dereferencing function result
(22 answers)
Closed 9 years ago.
Why does this code fail
function f(){
return array('k'=>'abc');
}
print_r(f()['k']);
and this code work?
function f(){
return array('k'=>'abc');
}
$a = f();
print_r($a['k']);
The only difference is the assignment of f's result to $a before trying to access elements of the array. How can I reference the result of f directly without the additional assignment?
This is only available in PHP 5.4. It is known as array dereferencing. Prior to 5.4 you have to store the return value and then access the array elements.
Docs
PHP 5.4.0 offers a wide range of new features:
Function array dereferencing has been added, e.g. foo()[0].
If you try this on any version below 5.4, you will get this error:
Parse error: syntax error, unexpected '[' on line ...