Last Updated: February 25, 2016
·
471
· evertonsilva

Retrieve an array value at a function call

When we have a function that returns a array, we can retrieve any specific value from that array just putting the key after the function call. Let's see:

function my_fn() {

   return array( 'key1'=>'value1', 
                  'key2'=>'value2' );
}

echo my_fn()['key1']; 

I have using this for a while and I like the way we can just pick up a value from the array without create a variable to handle the return value.

But what do you think? I wanna know if it is a good practice so please let your comments!

Have a fresh tip? Share with Coderwall community!

Post
Post a tip
Did you know?

PHP applications still send a browser full of JavaScript. Hardening the server does not inventory the payment, analytics, and plugin scripts that run after the HTML is delivered.

Security insights from Cside