Why doesn't array accessor syntax work directly on return value of a function?
Why does this work:
$optionArray = get_option('myplugin_options');
$optionArray = $optionArray['defaultButton'];
开发者_运维百科
But not this?
$optionArray = get_option('myplugin_options')['defaultButton'];
You can't access an array using that syntax when it's returned directly from a function like that, you'll get a syntax error in PHP. Other languages might let you do this sort of thing (I think Python does), but it's not correct in PHP (at least up to the current 5.3.x versions).
Wrapping the function in parentheses or curly braces doesn't help either, you'll still get a syntax error:
$x = (range(1, 10))[4];
var_dump($x);
gives the following error:
Parse error: syntax error, unexpected '[' in C:\Users\X\Desktop\example.php on line 2
Maybe in future versions of PHP it might be possible to access arrays like this, but for now, you're just going to have to assign them to variables first:
$x = range(1, 10);
var_dump($x[4]);
精彩评论