Ajit Bhandari
Ajit Bhandari

Reputation: 1169

Square bracket after parentheses in php function

When function return an array and we just need for particular key value can we use it like this:-

$obj->functionName($para)["x"];

is this proper way of doing this or should use variable?

Upvotes: 0

Views: 685

Answers (1)

Jagadesha NH
Jagadesha NH

Reputation: 2748

Yes it works fine, you don't have to make variable and waste memory.

<?php

function getArray() {
    return [ 'name1', 'name2', 'name3'];
}

echo getArray()[1];

?>

Upvotes: 2

Related Questions