-3

I found a PHP library in which a line is:

$this->get_soap_provider_options()['location'];

But this is producing error:

Parse error: syntax error, unexpected '[' in ...path to file.. at line...

I could not understand why ['location'] is written after function argument get_soap_provider_options(). It should be get_soap_provider_options('location') or get_soap_provider_options(array('location')) or something like this.

I think this line of code is for PHP 5.4 or higher. How can I write this line for an older version of PHP?

halfer
  • 19,824
  • 17
  • 99
  • 186
  • `get_soap_provider_options()` returns an array and you try to access a value (within the array) through the key `location`. – Tom Udding Jun 13 '17 at 06:59
  • http://php.net/manual/en/language.types.array.php#language.types.array.syntax.accessing, example 7. – u_mulder Jun 13 '17 at 07:00
  • Tom, how to write the $this->get_soap_provider_options()['location'] line for older version (before 5.4)? Because after closing argument braces, it is not allowed to write `['location']` in older version of php. It should be something like this: `$this->get_soap_provider_options('location')` or `$this->get_soap_provider_options(array('location'))` ....... how to write? – Abdullah Mamun-Ur- Rashid Jun 13 '17 at 07:03
  • Try to store the result from the function in a variable and access the value through the variable. – Tom Udding Jun 13 '17 at 07:06
  • I could not understand why ['location'] is written after function agrument get_soap_provider_options(). It should be get_soap_provider_options('location') or get_soap_provider_options(array('location')) or something like this. – Abdullah Mamun-Ur- Rashid Jun 13 '17 at 07:09
  • Possible duplicate of [Access array returned by a function in php](https://stackoverflow.com/questions/1459377/access-array-returned-by-a-function-in-php) – Tom Udding Jun 13 '17 at 07:10

1 Answers1

0

What you stated about it being for PHP >= 5.4 is correct. This syntax is used to access an array returned by a function. This is called array dereferencing (from a function or method).

To access the value from the array in PHP < 5.4 you'll have to store the result from the function or method in a variable. This variable will act as a "temporary" variable (see example 7 on the documentation):

$tmp = $this->get_soap_provider_options();
// now you can use > $tmp['location']; <
//                   ^^^^^^^^^^^^^^^^^
Tom Udding
  • 2,264
  • 3
  • 20
  • 30