views:

629

answers:

4

In every other programming language I use on a regular basis, it is simple to operate on the return value of a function without declaring a new variable to hold the function result.

In PHP, however, this does not appear to be so simple:

<?php 
function foobar(){
    return preg_split('/\s+/', 'zero one two three four five');
}

// can php say "zero"?

/// print( foobar()[0] ); /// <-- nope
/// print( &foobar()[0] );     /// <-- nope
/// print( &foobar()->[0] );     /// <-- nope
/// print( "${foobar()}[0]" );    /// <-- nope
?>

Can anyone suggest how to do this in PHP?

+11  A: 

PHP can not access array results from a function. Some people call this an issue, some just accept this as how the language is designed. So PHP makes you create unessential variables just to extract the data you need.

So you need to do.

$var = foobar();
print($var[0]);
Ólafur Waage
I realise that I'm still incredibly new to this, but why is this a problem? It...makes sense to me that you'd need to create a variable to hold a value/result; though admittedly: *very new*
David Thomas
Some people call this an issue, but this is just how the language is designed. Other languages are designed in a way where this is possible, and people coming from those languages feel that this is an issue.
Ólafur Waage
I honestly don't know why this is the case but it is.
cletus
Thanks for the clarity @ Ólafur =)
David Thomas
It's an issue because it becomes very easy to lose track of where you are if you have a function that returns a structured variable or object. For example, what if you have $data['tvshow']['episodes'][1]['description'] as a valid address in your variable?
dreftymac
@ricebowl: no, it doesn't make sense. It would make sens to have for example $gdver = gd_info()["GD Version"]. The problem is, that PHP is not object oriented.
vartec
@Ólafur: actually I don't think there is other language which would have similar "feature". AFAIR, even in plain C you can dereference function result. +1 anyway, because that's the only correct answer.
vartec
@vartec, what array dereferencing has to do with OOP?
Ionuț G. Stan
+7  A: 

Well, you could use any of the following solutions, depending on the situation:

function foo() {
    return array("foo","bar","foobar","barfoo","tofu");
}
echo(array_shift(foo())); // prints "foo"
echo(array_pop(foo())); // prints "tofu"

Or you can grab specific values from the returned array using list():

list($foo, $bar) = foo();
echo($foo); // prints "foo"
echo($bar); // print "bar"

Edit: the example code for each() I gave earlier was incorrect. each() returns a key-value pair. So it might be easier to use foreach():

foreach(foo() as $key=>$val) {
    echo($val);
}
Calvin
+1  A: 

Write a wrapper function that will accomplish the same. Because of PHP's easy type-casting this can be pretty open-ended:

function array_value ($array, $key) { return $array[$key]; }

Nolte Burke
A: 

Actually, I've written a library which allows such behavior:

http://code.google.com/p/php-preparser/

Works with everything: functions, methods. Caches, so being as fast as PHP itself :)

valya