Home >Backend Development >PHP Tutorial >How Do I Access Array Return Values from PHP Functions, Considering Private Data Limitations?
Accessing Array Return Value from a Function in PHP
In PHP, accessing the return value of an array from a function can be challenging when you encounter private data limitations. Let's explore a common scenario and provide solutions.
Consider the following function for testing a condition:
myfunction() { return '($this->data["a"]["b"] ? true : false)'; }
However, accessing the private $this->data property poses a problem. Assigning it to a temporary variable does not resolve the issue when using it directly in an if() block.
PHP 5.4 and Later
Since PHP 5.4, you can directly access array elements from a function return value without assigning to a variable:
getSomeArray()[2];
PHP 5.3 and Earlier
For PHP 5.3 or earlier, you'll need to create a temporary variable to hold the array:
$array = myfunction(); $array["a"]["b"];
The above is the detailed content of How Do I Access Array Return Values from PHP Functions, Considering Private Data Limitations?. For more information, please follow other related articles on the PHP Chinese website!