0

I don't mean for this question to be about Python vs PHP but about languages in general. I use Python and PHP as examples because I know them.

In Python we can do mytoken = mystring.split(mydelimiter)[1], accessing the list returned by str.split without ever assigning it to a list. In PHP we must put the array in memory before accessing it, as in $mytokenarray = explode($mydelimiter, $mystring); $mytoken = $mytokenarray[1];. As far as I know it is not possible to do this in one statement as in Python.

What is going on behind this difference?

BenMorel
  • 34,448
  • 50
  • 182
  • 322
sans
  • 2,159
  • 4
  • 20
  • 22
  • possible duplicate of [Parse error on explode('-','foo-bar')\[0\] (for instance)](http://stackoverflow.com/questions/2396782/parse-error-on-explode-foo-bar0-for-instance) – Ignacio Vazquez-Abrams Nov 09 '10 at 19:58
  • Yes, it is basically a duplicate of that question. – sans Nov 09 '10 at 20:01
  • [Array Dereferencing is in trunk now](http://schlueters.de/blog/archives/138-Features-in-PHP-trunk-Array-dereferencing.html) – Gordon Nov 09 '10 at 20:06
  • 1
    Functions are not indexable in any language that I've ever heard of. You mean "expressions". – John Machin Nov 09 '10 at 20:20
  • There are a lot of false assumptions in the question. The example is not indexing the function. It is indexing the return value of the function. In PHP assigning a variable to a value does not "put the array in memory before accessing it". The array is already in memory. – jrwren Feb 17 '13 at 16:54

3 Answers3

1

This feature is now possible with PHP as of version 5.4.

tiagosv
  • 23
  • 3
1

It is a design decision the authors of the languages chose to make. Generally spoken (and this is of course not always the case) the nicer the syntax a language has the slower it tends to be. Case in point: Ruby.

Jakub Hampl
  • 39,863
  • 10
  • 77
  • 106
  • This makes no sense. For any compiled language, of course, the syntax disappears at runtime. And for interpreted languages—well, take Ruby. A source file is loaded and parsed *before* it is run, so even if parsing *were* much slower (something I'm not convinced of), this would only be a problem *once per file*. And PHP here is not making things "nicer", just less regular. – Antal Spector-Zabusky Nov 09 '10 at 22:38
  • I wonder if it's more to the point to say that the nicer the syntax is the more difficult the compiler is to write. – Dave Aaron Smith Nov 11 '10 at 14:45
  • Niceness is a complicated thing. Some of it won't affect neither compiler nor speed, some will affect compiler complexity (affecting speed of interpreted languages) and some will affect runtime performance. Examples: I'd imagine that not requiring semicolons at the end of lines won't affect complexity nor performance since you just use the `\n` instead of `;`. Not requiring type declarations in a statically typed language increases compiler complexity since it has to infer the types. Dynamically typed languages are slower at runtime because method calls must be looked up at runtime. – Jakub Hampl Nov 11 '10 at 14:53
1

If you try to do this in php

$mytokenarray = explode($mydelimiter, $mystring)[1];

notice the error you get: Parse error: syntax error, unexpected '['.

This means that php is getting upset when it tries to parse the code, not when it tries to execute it. I suspect that means that php's grammar (which, I hear rumored, is generated on the fly though I really have no idea) says that you can't put '[' after a statement or expression or whatever they call it. Rather, you can probably only put '[' after a variable.

Here's Python's grammar. http://docs.python.org/reference/grammar.html which contains the rule trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME From there you can see that trailer is part of atom which can also contain [. You're starting to get the picture that it's pretty complicated.

Blah blah blah, long story short, learn about compilers, or even better, write one for a toy language. Then you will have loads of insight into language quirks.

Dave Aaron Smith
  • 4,517
  • 32
  • 37
  • 1
    Or, put more controversially: PHP's grammar is very weird/full of special cases. In sane languages, referencing a variable is just another expression... –  Nov 09 '10 at 20:17