Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to reference a specific element of an anonymous array in PHP?

This is probably a simple question, and I'm afraid the answer might be "no", but...

Here's a simple piece of code:

function func1() {
  $bt = debug_backtrace();
  print "Previous function was " . $bt[1]['function'] . "\n";
}

Now... Can this be done without the temporary variable? In another language, I might expect to be able to say:

function func1() {
  print "Previous function was " . (debug_backtrace())[1]['function'] . "\n";
}

Alas, in PHP, this results in an error:

PHP Parse error:  syntax error, unexpected '[' ...

If it can't be done, it can't be done, and I'll use a temporary variable, but I'd rather not.

like image 317
Rick Koshi Avatar asked Nov 26 '11 04:11

Rick Koshi


1 Answers

No, direct dereferencing is unfortunately not supported in current versions of PHP, but will apparently come in PHP 5.4.

Also see Terminology question on "dereferencing"?.

like image 51
deceze Avatar answered Sep 21 '22 04:09

deceze