7

Why can't I immediately access elements in the array returned by explode()?

For example, this doesn't work:

$username = explode('.',$thread_user)[1]; 
//Parse error: syntax error, unexpected '[

But this code does:

$username = explode('.',$thread_user); 
$username = $username[1];

I don't usually program in PHP, so this is rather confusing to me.

Mike Atlas
  • 8,193
  • 4
  • 46
  • 62

6 Answers6

6

The reason it isn't obvious how to do what you want is that explode could return false. You should check the return value before indexing into it.

James McLeod
  • 2,381
  • 1
  • 17
  • 19
  • Thank you. I am not used to return values behaving like that and I missed that when I checked the PHP manual. – Mike Atlas Feb 15 '10 at 21:51
  • It's always seemed a little quirky to me, but it is useful, and no uglier than e.g. having to check calls to malloc in C for NULL returns. – James McLeod Feb 15 '10 at 22:06
  • 2
    It's actually just that the PHP syntax doesn't support this. You're simply saying it's intended that way to make people check their return values? – falstro Feb 15 '10 at 22:09
  • 4
    No, not exactly. Rather, I think this is one of the most useful ramifications of the decision not to support this syntax. – James McLeod Feb 15 '10 at 22:11
  • 2
    James, you're absolutely right that for PHP to support this syntax would add another layer of complexity owing to the fact that in PHP, a soft-typed language, you cannot guarantee the return type of a function. However there are other cases where PHP's parser does in fact allow such conditions may occur. In these cases, PHP simply attempts the closest typecast it can manage and throws a warning message. Try accessing `$userID[1]` where $userID is an integer, for example. – Brian Lacy Feb 16 '10 at 00:38
5

It's version dependent. PHP 5.4 does support accessing the returned array.

Source: http://php.net/manual/en/language.types.array.php#example-115

Carlos F
  • 190
  • 3
  • 10
avetarman
  • 1,244
  • 9
  • 8
4

Actually, PHP simply does not support this syntax. In languages like Javascript (for instance), the parser can handle more complex nesting/chaining operations, but PHP is not one of those languages.

Brian Lacy
  • 18,785
  • 10
  • 55
  • 73
  • 2
    ^ the real answer. Even if you write a function that returns an invariant array, you still can't index the function call (as James' answer might lead you to believe), because it's simply a matter of the syntax not working. – Chuck Feb 15 '10 at 22:42
  • +1 for Chuck's comment - clearly some defensive programming has allowed me to avoid learning PHP as fully as I should. (and maybe I shouldn't try to answer questions on the last day of a three-day weekend...) – James McLeod Feb 15 '10 at 22:59
2

Since explode() returns an array, you may use other functions such as $username = current(explode('.',$thread_user));

qualbeen
  • 1,534
  • 4
  • 16
  • 27
1

I just use my own function:

function explodeAndReturnIndex($delimiter, $string, $index){
    $tempArray = explode($delimiter, $string);
    return $tempArray[$index];
}

the code for your example would then be:

$username = explodeAndReturnIndex('.', $thread_user, 1);
budiDino
  • 13,044
  • 8
  • 95
  • 91
1

Here's how to get it down to one line:

$username = current(array_slice(explode('.',$thread_user), indx,1));

Where indx is the index you want from the exploded array. I'm new to php but I like saying exploded array :)

ragamufin
  • 4,113
  • 30
  • 32