This is a discussion on Why is some_function()[some_index] invalid syntax? within the PHP General forums, part of the PHP Programming Forums category; Hi there, Why is it that if I try to evaluate an index of an array returned by a function ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
Arlen Christian Mart Cuss wrote:
> Hi there, > > Why is it that if I try to evaluate an index of an array returned by a > function immediately, a syntax error is produced? (unexpected '[', > expecting ',' or ';') > > Thanks, > Arlen. > I asked that question years ago. It was explained to me that php does not have, what is called, messaging. Something that is in lower level languages. |
|
|||
|
On Jan 10, 2008, at 8:00 PM, Arlen Christian Mart Cuss <celtic@sairyx.org
> wrote: > Hi there, > > Why is it that if I try to evaluate an index of an array returned by a > function immediately, a syntax error is produced? (unexpected '[', > expecting ',' or ';') > > Thanks, > Arlen. I've run into this problem. (It works in Javascript >.>) While I don't know why, you could store it in a temporary variable or use the list() language construct. -Casey |
|
|||
|
On Jan 10, 2008 11:00 PM, Arlen Christian Mart Cuss <celtic@sairyx.org>
wrote: > Hi there, > > Why is it that if I try to evaluate an index of an array returned by a > function immediately, a syntax error is produced? (unexpected '[', > expecting ',' or ';') thats hillarious, i literally brought this up at the office like 2 days ago. ill tell you why its really lame (imho), because php5 supports syntax like this: function someFunc() { return date_create(); } echo someFunc()->format('Y-m-d'); that is, it allows you to chain a method invocation to the invocation of a function if the function returns an object. -nathan |
|
|||
|
Nathan Nobbe wrote:
> On Jan 10, 2008 11:00 PM, Arlen Christian Mart Cuss <celtic@sairyx.org> > wrote: > >> Hi there, >> >> Why is it that if I try to evaluate an index of an array returned by a >> function immediately, a syntax error is produced? (unexpected '[', >> expecting ',' or ';') > > > thats hillarious, i literally brought this up at the office like 2 days ago. > ill tell you why its really lame (imho), because php5 supports syntax > like this: > function someFunc() { > return date_create(); > } > echo someFunc()->format('Y-m-d'); > > that is, it allows you to chain a method invocation to the invocation of a > function > if the function returns an object. > > -nathan > So, make all your functions return objects, and have the object have a method called get or index or something like that that returns the index requested. :) Better yet, make everything an object: String, Numeric, Array, etc |
|
|||
|
On Jan 11, 2008 12:25 AM, Jim Lucas <lists@cmsws.com> wrote:
> So, make all your functions return objects, and have the object have a > method called get or index or something like that that returns the index > requested. :) > > Better yet, make everything an object: String, Numeric, Array, etc > i like using stdClass as a container sometimes, however it doesnt have the plethora of utility functions that arrays do :( there are workarounds of course, but obviously i just store the result to a variable and subsequently use that. this is just one of those little things picky, non-commiters like myself bitch about :) -nathan |
|
|||
|
2008. 01. 10, csütörtök keltezéssel 21.25-kor Jim Lucas ezt Ã*rta:
> Nathan Nobbe wrote: > > On Jan 10, 2008 11:00 PM, Arlen Christian Mart Cuss <celtic@sairyx.org> > > wrote: > > > >> Hi there, > >> > >> Why is it that if I try to evaluate an index of an array returned by a > >> function immediately, a syntax error is produced? (unexpected '[', > >> expecting ',' or ';') > > > > > > thats hillarious, i literally brought this up at the office like 2 days ago. > > ill tell you why its really lame (imho), because php5 supports syntax > > like this: > > function someFunc() { > > return date_create(); > > } > > echo someFunc()->format('Y-m-d'); > > > > that is, it allows you to chain a method invocation to the invocation of a > > function > > if the function returns an object. > > > > -nathan > > > > So, make all your functions return objects, and have the object have a > method called get or index or something like that that returns the index > requested. :) > > Better yet, make everything an object: String, Numeric, Array, etc and call it Java ;) greets Zoltán Németh > |
|
|||
|
Arlen Christian Mart Cuss schreef:
> Hi there, > > Why is it that if I try to evaluate an index of an array returned by a > function immediately, a syntax error is produced? (unexpected '[', > expecting ',' or ';') because it's not valid syntax. strangely enough php is neither <insert other language here>, nor is it capable of determine valid syntax based on a mind meld with the developer that wrote the code in question. but you knew that already really. > > Thanks, > Arlen. > |
|
|||
|
Nathan Nobbe wrote:
> On Jan 11, 2008 3:45 AM, Zoltán Németh <znemeth@alterationx.hu> wrote: > >> and call it Java ;) >> > > or perhaps javascript :) > function cool() { > return [1, 2, 3]; > } > alert(cool()[0]); > > -nathan > or Ruby -- Jim Lucas "Some men are born to greatness, some achieve greatness, and some have greatness thrust upon them." Twelfth Night, Act II, Scene V by William Shakespeare |
![]() |
| Thread Tools | |
| Display Modes | |
|
|