0

I am trying to use the PHP error_get_last() function, but my question goes more to general handling of arrays in PHP.

error_get_last()

returns an array, but I only want the 'message' element from it.

So this works:

$ErrData = error_get_last();
$ErrMsg = $ErrData['message'];

but this is a syntax error:

$ErrMsg = error_get_last()['message'];

Can someone explain why, if error_get_last() returns an array, the elements cannot be referenced directly?

Surely I don't have to create unwanted temporary array variables whenever I want to access elements like this?

Shikiryu
  • 10,180
  • 8
  • 49
  • 75
stephen
  • 303
  • 1
  • 8
  • 2
    This is possible since PHP 5.4. In php versions before that, trying to access a element from something returned by a function is a syntax error. – Sumurai8 Dec 05 '13 at 21:33
  • Good pointer, but that answer states should be possible for PHP 5.4+. I have 5.4.17 and still won't accept this syntax. – stephen Dec 05 '13 at 21:33
  • @stephen It's working great here in PHP 5.4: http://3v4l.org/7vYm6 – jszobody Dec 05 '13 at 21:42
  • Are you _very_ sure that's your PHP version? It works here in 5.4.21 – Wrikken Dec 05 '13 at 21:43
  • My apologies, you are all correct, it *is* working OK on my 5.4.17. I was having this problem yesterday, and since upgraded to OSX Mavericks which must have brought a new version of PHP with it. Thanks for the comments. – stephen Dec 05 '13 at 21:48
  • @Sumurai8, thanks for the link to the article, amusing and therapeutic for the times when PHP leaves me baffled. I am not a fan of weakly-typed interpreted languages generally, and prefer to live in .NET land. But still we must always be prepared to work outside of our comfort zone... – stephen Dec 05 '13 at 21:52

0 Answers0