Why is some_function()[some_index] invalid syntax?

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 ...


Go Back   Usenet Forums > PHP Programming Forums > PHP General

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 01-11-2008
Arlen Christian Mart Cuss
 
Posts: n/a
Default Why is some_function()[some_index] invalid syntax?

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.
Reply With Quote
  #2 (permalink)  
Old 01-11-2008
Jim Lucas
 
Posts: n/a
Default Re: [PHP] Why is some_function()[some_index] invalid syntax?

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.
Reply With Quote
  #3 (permalink)  
Old 01-11-2008
Casey
 
Posts: n/a
Default Re: [PHP] Why is some_function()[some_index] invalid syntax?

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
Reply With Quote
  #4 (permalink)  
Old 01-11-2008
Nathan Nobbe
 
Posts: n/a
Default Re: [PHP] Why is some_function()[some_index] invalid syntax?

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

Reply With Quote
  #5 (permalink)  
Old 01-11-2008
Jim Lucas
 
Posts: n/a
Default Re: [PHP] Why is some_function()[some_index] invalid syntax?

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
Reply With Quote
  #6 (permalink)  
Old 01-11-2008
Nathan Nobbe
 
Posts: n/a
Default Re: [PHP] Why is some_function()[some_index] invalid syntax?

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

Reply With Quote
  #7 (permalink)  
Old 01-11-2008
Zoltán Németh
 
Posts: n/a
Default Re: [PHP] Why is some_function()[some_index] invalid syntax?

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

>

Reply With Quote
  #8 (permalink)  
Old 01-11-2008
Nathan Nobbe
 
Posts: n/a
Default Re: [PHP] Why is some_function()[some_index] invalid syntax?

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

Reply With Quote
  #9 (permalink)  
Old 01-11-2008
Jochem Maas
 
Posts: n/a
Default Re: [PHP] Why is some_function()[some_index] invalid syntax?

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.
>

Reply With Quote
  #10 (permalink)  
Old 01-11-2008
Jim Lucas
 
Posts: n/a
Default Re: [PHP] Why is some_function()[some_index] invalid syntax?

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
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 10:35 PM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0