tags:

views:

128

answers:

5

Why does this code not work?

echo explode("?", $_SERVER["REQUEST_URI"])[0];

It says syntax error, unexpected '['.

Oddly, this works:

$tmp = explode("?", $_SERVER["REQUEST_URI"]);
echo $tmp[0];

But I really want to avoid to create such a $tmp variable here.

How do I fix it?


After the helpful answers, some remaining questions: Is there any good reason of the design of the language to make this not possible? Or did the PHP implementors just not thought about this? Or was it for some reason difficult to make this possible?

+5  A: 

Unlike Javascript, PHP can't address an array element after a function. You have to split it up into two statements or use array_slice().

stillstanding
Yea that seems so. :) But why? Is there any good reason of the design of the language? Or did the PHP implementors just not thought about this? Or was it for some reason difficult to make this possible?
Albert
You can use current() and next() functions for 0 and 1 elements too.
Anpher
Or use `list` on the left hand side.
strager
+1  A: 

Take a look at this previous question.

Hoohaah's suggestion of using strstr() is quite nice. The following will return from the beginning of the string until the first ? (the third argument for strstr() is only available for PHP 5.3.0 onward):

echo strstr($_SERVER["REQUEST_URI"], "?", TRUE);

Or if you want to stick with explode, you can make use if list(). (The 2 indicates that at most 2 elements will be returned by explode).

list($url) = explode("?", $_SERVER["REQUEST_URI"], 2);
echo $url;

Finally, to use the natively available PHP URL parsing;

$info = parse_url($_SERVER["REQUEST_URI"]);
echo $info["scheme"] . "://" . $info["host"] . $info["path"];
Peter Ajtai
He's trying to remove it, not recover it.
Artefacto
Thanks for the hint. Although, the only thing I want is to get just everything before the first `?`, so I guess `explode` is just easier here, isn't it?
Albert
Peter Ajtai
@Albert: In that case, just use `strstr($_SERVER['REQUEST_URI'],'?',TRUE)`
stillstanding
@hoohah - That's a great idea (for PHP 5.3.0 onward).
Peter Ajtai
+3  A: 

It's a (silly) limitation of the current PHP parser that your first example doesn't work. However, supposedly the next major version of PHP will fix this.

J Cooper
A: 

EDIT:

echo current(explode("?",  $_SERVER["REQUEST_URI"]));
NAVEED
array_shift should be passed an argument by reference.
Artefacto
what about current() ?
NAVEED
@NAVEED Same thing. See the [manual](http://pt.php.net/current).
Artefacto
I did not understand the problem. Can you explain in detail? For example I have a current URL `http://test.dev/index.php?input=10`. When I use `echo current( explode("?", $_SERVER["REQUEST_URI"]) );` then the output is `/index.php`. What is the problem here and what exactly result is required ?
NAVEED
+4  A: 

This is only allowed in the development branch of PHP (it's a new feature called "array dereferencing"):

echo explode("?", $_SERVER["REQUEST_URI"])[0];

You can do this

list($noQs) = explode("?", $_SERVER["REQUEST_URI"]);

or use array_slice/temp variable, like stillstanding said. You shouldn't use array_shift, as it expects an argument passed by reference.

Artefacto
-1 He don't want to use extra variable(temp) and want to access it directly. What is the difference between $noQs(in your anwser) and $temp(in his question) here.
Awan