5

In Javascript, after executing a function I can immediately get the an element of the array returned by the function, like so:

myFunc("birds")[0] //gets element zero returned from "myFunc()"

This is much easier and faster than doing this:

$myArray = myFunc("birds");
echo $myArray[0];

Does PHP have a similar shorthand to javascript? I'm just curious. Thanks in advance!

1
  • 1
    As of PHP 5.4, your syntax for option 1 is perfectly valid. Commented Feb 11, 2013 at 5:52

3 Answers 3

4

No, unfortunately in PHP you can only subscript an array variable, no other kind of array returning expression.

Sign up to request clarification or add additional context in comments.

Comments

1
reset(myFunc("birds"))

Will work nicely, although it doesn't work on associative arrays.

Or, of course, you could write a function, e.g.

function arr_get($arr, $i)
{
 return $arr[$i];
}

echo arr_get(myFunc("birds"), 0);

Comments

0

Short answer is no.

Longer answer is maybe coming soon. See:

Last time I used the PHP trunk, it worked fine.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.