php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #42440 Unable to access to a member of an array returned by a function
Submitted: 2007-08-27 10:00 UTC Modified: 2007-08-27 13:14 UTC
From: romain dot tartiere at healthgrid dot org Assigned:
Status: Not a bug Package: Arrays related
PHP Version: 5.2.3 OS: GNU/Linux
Private report: No CVE-ID: None
 [2007-08-27 10:00 UTC] romain dot tartiere at healthgrid dot org
Description:
------------
When using a function that returns an array, the parser is not apple to correctly parse the expression and assign the n'th element to a variable

Reproduce code:
---------------
<?php

function foo() {
  return array(1, 2, 3);
}

echo foo()[1]; # <<<

?>

Expected result:
----------------
2 (second element of the array)

Actual result:
--------------
Parse error: syntax error, unexpected '[', expecting ',' or ';'


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2007-08-27 10:22 UTC] jani@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php


 [2007-08-27 12:42 UTC] romain dot tartiere at healthgrid dot org
Thank you for your personalised answer and taking time to read and understand my bug report.

This is obviously a bug: even the "array(1,2,3)[1]" expression cannot be parsed by PHP!

Maybe it is just a LALR parser mistake like...

stuff: [...]
     | variable '[' expression ']'

... instead of ...

stuff: [...]
     | expression '[' expression ']'


Sorry for being rude, but your copy-paste-reply make me feel my problem has been underestimated by an inexperienced person. But maybe I am wrong, then just prove me that what I am talking about is nonsense...

According to me, if I can't do "a[i]" but can do "b = a; b[i]", there is something wrong.
 [2007-08-27 13:14 UTC] scottmac@php.net
It's a Duplicate of bug #23022
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri May 10 14:01:33 2024 UTC