0

Hello please help me out regarding returning the variable from the function I am return three variable from the function likw this

return $data 
return $success 
return $error

calling from

$check  =$user->search($result);

now to put some condition according to the variable received .The problem i am facing is that how can i get the name of the vairable which i have received . i want to receive it by name not by value like

if($check == $data )
umar
  • 3,073
  • 5
  • 35
  • 45
  • possible duplicate of [PHP: Is it possible to return multiple values from a function?](http://stackoverflow.com/questions/3579892/php-is-it-possible-to-return-multiple-values-from-a-function) and a [couple of others](http://stackoverflow.com/search?q=multiple+return+values+[php]). – Gordon Mar 22 '11 at 15:45
  • Use Python and you can return a tuple and assign the elements of it using a sane syntax ;) – ThiefMaster Mar 22 '11 at 15:47

6 Answers6

3

You cannot return more than one variable from a function. I suggest making an array, and returning that.

return array('data' => $data, 'success' => $success, 'error' => $error);
gen_Eric
  • 223,194
  • 41
  • 299
  • 337
3
function myfunc() {
   ... do stuff ...
   $values = array($data, $success, $error);
   return($values);
}

$data = myfunc();
echo "Data is ", $data[0];

return can only return a single thing, and it "aborts" the function, so you cannot have 2 or more returns in a row - only the first one would ever execute. So, the workaround is to return a container which contains all the values you need. In this case, i've used an array, but return can "return" anything - you could have an object passed back if you'd like.

Marc B
  • 356,200
  • 43
  • 426
  • 500
3

You can't return three variables that way. $success and $error will never be returned, because the function ends after the first return $data.

If you want to return three values, you can do it like this:

return array($data, $success, $error);

And call it like this:

list($data, $success, $error) = $user->search($result);
Nathan Ostgard
  • 8,258
  • 2
  • 27
  • 19
2

I don't think the original poster is trying to return 3 variables, but rather has multiple control paths and returns something different based on some conditions.

public function search($r) {
    ...
    return array('data'    => $data);
    ...
    return array('success' => $success);
    ...
    return array('error'   => $error);
}

$check = $user->search($result);
switch(key($check)) {
    case 'data':
        // handle data
    break;

    case 'success':
        // handle success
    break;

    case 'error':
        // handle error
    break;
}
Daniel Sloof
  • 12,568
  • 14
  • 72
  • 106
1

The clean solution would be to return a dataset (e.g. an array) on success, and throw an exception if an error occurs. Using this method would eliminate the need for the variable $success.

When you call the method implemented this way, you can go like this:

try
{
    $data = $someObject->someFunction($some_parameter);
}
catch(Exception $e)
{
    // error handling code path
}
Imi Borbas
  • 3,683
  • 1
  • 19
  • 16
-1

I'm not quite sure about your use case, but from the looks of the variables you expect, I would advise to turn them into reference parameters:

function search($data, &$success, &$error) {

This is what many native PHP functions do, and an applicable idiom for status variables like yours. The disadvantage is that you always have to invoke the method with the extra parameters then:

$check  = $user->search($result, $success, $error);

Does not address your (not solvable in the simplistic manner you tried) ..how can i get the name of the vairable which i have received, but I'm not quite sure what you meant or intended there.

mario
  • 144,265
  • 20
  • 237
  • 291