253

Is it possible to have a function with two returns like this:

function test($testvar)
{
  // Do something

  return $var1;
  return $var2;
}

If so, how would I be able to get each return separately?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
vincent
  • 2,715
  • 3
  • 17
  • 9
  • 1
    What exactly are you trying to accomplish? Surely if you explained your real problem, someone here could help you arrive at an elegant solution. – Mike Daniels Aug 10 '10 at 17:55
  • 3
    Question does not differentiate between either/or one of two values, both two of two values, or new concept lazy evaluation one then possibly two of two values. The first is trivial with any kind of conditional flow. The second is permitted in python: q, r = divmod(x, y); as well as Lisp; PHP requires the list($q,$r)=twovals(); hack, where function twovals(){ return array($a, $b); }. Lazy evaluation is rather advanced and has not caught on in PHP yet. Since the question is not precise, recommend not using this entry as definitive reference for this topic. – DragonLord Aug 15 '12 at 22:54
  • 8
    If you need both the values, return them in an array. – Bhargav Nanekalva Nov 12 '15 at 15:33
  • 3
    @DragonLord in PHP 7.1, you can use the [short list syntax](https://wiki.php.net/rfc/short_list_syntax) – Janus Troelsen Nov 14 '16 at 15:37
  • 1
    There is a duplicate question, but with more concise answers thus it will get you faster to the point: [Returning 2 values from a function](http://stackoverflow.com/questions/3815234/returning-2-values-from-a-function). – Gras Double Mar 25 '17 at 21:46
  • Maybe is a good idea to separate your function into 2 functions so they return one value each. That's the way I solved the same problem and I think it may be smarter – gtamborero Sep 26 '18 at 07:46
  • @Gras Double: Agreed. This question is ambiguous (as is evident in the different kind of answers). Is the real intent to return two values from one function call (like the duplicate) or is it to return one or the another (single) value? It is probably the first, but the OP has never responded (in the 20 days the OP was active on this site). – Peter Mortensen Dec 12 '19 at 20:45
  • @Janus Troelsen: [*Marek Gralikowski* and](https://stackoverflow.com/a/48166310) [*Nukesor* provided examples](https://stackoverflow.com/a/48166310) – Peter Mortensen Dec 12 '19 at 21:26

32 Answers32

495

Technically, you can't return more than one value. However, there are multiple ways to work around that limitation. The way that acts most like returning multiple values, is with the list keyword:

function getXYZ()
{
    return array(4,5,6);
}

list($x,$y,$z) = getXYZ();

// Afterwards: $x == 4 && $y == 5 && $z == 6
// (This will hold for all samples unless otherwise noted)

Technically, you're returning an array and using list to store the elements of that array in different values instead of storing the actual array. Using this technique will make it feel most like returning multiple values.

The list solution is a rather php-specific one. There are a few languages with similar structures, but more languages that don't. There's another way that's commonly used to "return" multiple values and it's available in just about every language (in one way or another). However, this method will look quite different so may need some getting used to.

// note that I named the arguments $a, $b and $c to show that
// they don't need to be named $x, $y and $z
function getXYZ(&$a, &$b, &$c)
{
    $a = 4;
    $b = 5;
    $c = 6; 
}

getXYZ($x, $y, $z);

This technique is also used in some functions defined by php itself (e.g. $count in str_replace, $matches in preg_match). This might feel quite different from returning multiple values, but it is worth at least knowing about.

A third method is to use an object to hold the different values you need. This is more typing, so it's not used quite as often as the two methods above. It may make sense to use this, though, when using the same set of variables in a number of places (or of course, working in a language that doesn't support the above methods or allows you to do this without extra typing).

class MyXYZ
{
    public $x;
    public $y;
    public $z;
}

function getXYZ()
{
    $out = new MyXYZ();
    
    $out->x = 4;
    $out->y = 5;
    $out->z = 6;
    
    return $out;
}

$xyz = getXYZ();

$x = $xyz->x;
$y = $xyz->y;
$z = $xyz->z;

The above methods sum up the main ways of returning multiple values from a function. However, there are variations on these methods. The most interesting variations to look at, are those in which you are actually returning an array, simply because there's so much you can do with arrays in PHP.

First, we can simply return an array and not treat it as anything but an array:

function getXYZ()
{
    return array(1,2,3);
}

$array = getXYZ();

$x = $array[0];
$y = $array[1];
$z = $array[2];

The most interesting part about the code above is that the code inside the function is the same as in the very first example I provided; only the code calling the function changed. This means that it's up to the one calling the function how to treat the result the function returns.

Alternatively, one could use an associative array:

function getXYZ()
{
    return array('x' => 4,
                 'y' => 5,
                 'z' => 6);
}

$array = getXYZ();

$x = $array['x'];
$y = $array['y'];
$z = $array['z'];

Php does have the compact function that allows you to do same as above but while writing less code. (Well, the sample won't have less code, but a real world application probably would.) However, I think the amount of typing saving is minimal and it makes the code harder to read, so I wouldn't do it myself. Nevertheless, here's a sample:

function getXYZ()
{
    $x = 4;
    $y = 5;
    $z = 6;
    
    return compact('x', 'y', 'z');
}

$array = getXYZ();

$x = $array['x'];
$y = $array['y'];
$z = $array['z'];

It should be noted that while compact does have a counterpart in extract that could be used in the calling code here, but since it's a bad idea to use it (especially for something as simple as this) I won't even give a sample for it. The problem is that it will do "magic" and create variables for you, while you can't see which variables are created without going to other parts of the code.

Finally, I would like to mention that list doesn't really play well with associative array. The following will do what you expect:

function getXYZ()
{
    return array('x' => 4,
                 'y' => 5,
                 'z' => 6);
}

$array = getXYZ();

list($x, $y, $z) = getXYZ();

However, the following will do something different:

function getXYZ()
{
    return array('x' => 4,
                 'z' => 6,
                 'y' => 5);
}

$array = getXYZ();

list($x, $y, $z) = getXYZ();

// Pay attention: $y == 6 && $z == 5

If you used list with an associative array, and someone else has to change the code in the called function in the future (which may happen just about any situation) it may suddenly break, so I would recommend against combining list with associative arrays.

ToolmakerSteve
  • 18,547
  • 14
  • 94
  • 196
Jasper
  • 11,590
  • 6
  • 38
  • 55
  • 2
    Also: return compact('var1', 'var2', 'var3'); – bjudson Aug 26 '10 at 22:37
  • 1
    That is another option, but it is doesn't feel to me as returning multiple values, just as returning an array. That may just be me though. Personally, I would find `return array('x' => $x, 'y' => $y, 'z' => $z)` cleaner, but to the point where I would write it myself, not to the point where I would ask others to use that format. – Jasper Aug 26 '10 at 22:54
  • 4
    Using List() is a great answer to a simular problem I had. this is an excellent way validate and return multiple variables back into a function. a quick look at the php docs will shed more light on this function and perhaps make it more clear. http://php.net/manual/en/function.list.php .. thanks Jasper! – JustinP Oct 19 '12 at 20:22
  • +1 nice answer ... but i am afraid it will be deleted due to question .. :( – NullPoiиteя Jul 19 '13 at 14:01
  • 3
    +1 for such an extensive answer, especially to such a broad, general question. Despite that, this answer has helped me immensely. – Lee Blake Sep 02 '14 at 18:15
  • 1
    I'd HIGHLY advise against modifying function parameters by reference. Leads to unexpected behaviors when other people use your function. – earl3s Sep 02 '16 at 22:48
  • 2
    @earl3s Document the behavior, it's a common paradigm. As you can see with the two built-in functions I linked to, it's a thing that php itself does as well. Of course, you should generally not modify variables of which the value serves as input, but having variables of which the initial value is not used and for which the only purpose is setting its value as sort of an output is a really normal thing to do. Just be sure to document it as such (if it's an external thing). – Jasper Sep 03 '16 at 21:53
  • 2
    @Mikey Historical reasons. This answer was originally posted on a different question, which was deleted for being an exact duplicate of this one (despite the fact that it was actually older). The answer was moved to this question later than the last visit of the one who asked this question. – Jasper May 01 '17 at 08:42
  • 1
    As of today there is also another syntax since php 7.1: `[]` ... https://wiki.php.net/rfc/short_list_syntax – davidmpaz Oct 19 '18 at 08:07
  • @davidmpaz That's quite cool. I'll try to find some time to incorporate it into my answer. – Jasper Oct 19 '18 at 22:22
  • A different syntax issue, when you are using this for an associative array `$x = $array['x']`, you can do it without the quotes around the array key value. So `$x = $array[x]` will work. Less typing, prevents coloring as a string in your text editor, and all around looks more consistent, imo. – user1934286 Jan 28 '23 at 00:20
  • @user1934286 Please do not do that. It will generate a warning and that's because it relies on weird legacy behavior of php that is also quite brittle. (Namely, if php encounters the usage of an undefined constant, it will emit a warning and use the name of the constant as its value. If someone decides to define the constant - possibly in an entirely different file - your code will suddenly no longer work as intended.) – Jasper Feb 01 '23 at 16:08
183

There is no way of returning 2 variables. Although, you can propagate an array and return it; create a conditional to return a dynamic variable, etc.

For instance, this function would return $var2

function wtf($blahblah = true) {
    $var1 = "ONe";
    $var2 = "tWo";

    if($blahblah === true) {
      return $var2;
    }
    return $var1;
}

In application:

echo wtf();
//would echo: tWo
echo wtf("not true, this is false");
//would echo: ONe

If you wanted them both, you could modify the function a bit

function wtf($blahblah = true) {
    $var1 = "ONe";
    $var2 = "tWo";

    if($blahblah === true) {
      return $var2;
    }

    if($blahblah == "both") {
      return array($var1, $var2);
    }

    return $var1;
}

echo wtf("both")[0]
//would echo: ONe
echo wtf("both")[1]
//would echo: tWo

list($first, $second) = wtf("both")
// value of $first would be $var1, value of $second would be $var2
dockeryZ
  • 3,981
  • 1
  • 20
  • 28
  • 15
    If only PHP had Perl's `wantarray()` – Marc B Aug 26 '10 at 22:22
  • 12
    IMHO this answer would be improved if it omitted the first part, which discusses how to return one *or* a different value, depending on some condition. I'm certain that 99.999+% of the people coming to this discussion want to know how to return *both* values at the same time. See the highest voted answer. – ToolmakerSteve Apr 03 '19 at 21:22
  • 1
    @MarcB If Only PHP and Perl had Pythons automatic tupling/untupling – `return a, b, c` and `a, b, c = func()` – Nils Lindemann Jul 24 '20 at 09:01
  • 2
    @NilsLindemann: `php 7.1` does, via array destructuring syntax: `return [$a, $b, $c];` and `[$x, $y, $z] = func();`. (That is a trivial number of characters more than the Python syntax, and is the intended way to achieve that result in PHP. And really, PHP always had equivalent functionality, it just required slightly more verbose syntax.) – ToolmakerSteve Dec 21 '20 at 23:16
83

In your example, the second return will never happen - the first return is the last thing PHP will run. If you need to return multiple values, return an array:

function test($testvar) {

    return array($var1, $var2);
}

$result = test($testvar);
echo $result[0]; // $var1
echo $result[1]; // $var2
Tim Fountain
  • 33,093
  • 5
  • 41
  • 69
78

Since PHP 7.1 we have proper destructuring for lists. Thereby you can do things like this:

$test = [1, 2, 3, 4];
[$a, $b, $c, $d] = $test;
echo($a);
> 1
echo($d);
> 4

In a function this would look like this:

function multiple_return() {
    return ['this', 'is', 'a', 'test'];
}

[$first, $second, $third, $fourth] = multiple_return();
echo($first);
> this
echo($fourth);
> test

Destructuring is a very powerful tool. It's capable of destructuring key=>value pairs as well:

["a" => $a, "b" => $b, "c" => $c] = ["a" => 1, "b" => 2, "c" => 3];

Take a look at the new feature page for PHP 7.1:

New features

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Nukesor
  • 945
  • 1
  • 7
  • 13
  • 7
    I wish StackOverflow would have a feature like 'featured answer' even when is not the accepted by the time of the creation of the question because this answer here is quite useful and updated, but it's off-topic, of course. – Jonatas CD Mar 09 '19 at 20:43
  • 3
    @JonatasCD - not sure why you say this answer is "off-topic". In php 7.1 it *is* the most convenient way to create and handle multiple return values from a function. So for newer php versions, its a superior answer to the original accepted answer. – ToolmakerSteve Apr 03 '19 at 21:13
  • @ToolmakerSteve I think you misunderstood me. "off-topic" to the question was my suggestion of being able to change what was the accepted question based on future implementations. It was nothing against your answer ;) – Jonatas CD Apr 04 '19 at 15:10
  • @JonatasCD - Ahh that explains it. (Isn't my answer, I was just puzzled.) At least a tag that says "this is more up-to-date than the accepted answer" Maybe three people have to agree with that tag, and then it becomes featured. :) – ToolmakerSteve Apr 04 '19 at 18:31
28

In PHP 5.5 there is also a new concept: generators, where you can yield multiple values from a function:

function hasMultipleValues() {
    yield "value1";
    yield "value2";
}

$values = hasMultipleValues();
foreach ($values as $val) {
    // $val will first be "value1" then "value2"
}
SztupY
  • 10,291
  • 8
  • 64
  • 87
21

For PHP 7.1.0 onwards, you can use the new syntax (instead of the list function):

/**
* @return  array  [foo, bar]
*/
function getFooAndBar(): array {
    return ['foo', 'bar'];
}

[$foo, $bar] = getFooAndBar();

print 'Hello '. $foo . ' and ' . $bar;

It's OK for me if you want to return 2-3 variables, otherwise you should use an object with the desired properties.

Rahil Wazir
  • 10,007
  • 11
  • 42
  • 64
20

Or you can pass by reference:

function byRef($x, &$a, &$b)
{
    $a = 10 * $x;
    $b = 100 * $x;
}

$a = 0;
$b = 0;

byRef(10, $a, $b);

echo $a . "\n";
echo $b;

This would output

100
1000
Jake N
  • 10,535
  • 11
  • 66
  • 112
10

I know that I am pretty late, but there is a nice and simple solution for this problem.
It's possible to return multiple values at once using destructuring.

function test()
{
    return [ 'model' => 'someValue' , 'data' => 'someothervalue'];
}

Now you can use this

$result = test();
extract($result);

extract creates a variable for each member in the array, named after that member. You can therefore now access $model and $data

toddmo
  • 20,682
  • 14
  • 97
  • 107
Muhammad Raheel
  • 19,823
  • 7
  • 67
  • 103
  • 1
    NOTE: be careful that the keys (here `model` and `data`) don't already exist as variables. If they do, use the `prefix` parameter of `extract` to avoid conflicts. – ToolmakerSteve Apr 03 '19 at 21:17
8

PHP 7.1 Update

Return an array.

function test($testvar)
{
  // Do something
  return [$var1, $var2];
}

then use that like below:

[$value1, $value2] = test($testvar);
Hossein
  • 3,755
  • 2
  • 29
  • 39
  • 2
    This is the same answer as https://stackoverflow.com/a/58157232/51685 just 2 replies below this one. – AKX Sep 24 '21 at 08:12
  • Do not upvote this answer - it's a duplicate of [this one](https://stackoverflow.com/a/58157232/1191147) posted 2 years earlier. – Hashim Aziz Aug 02 '23 at 17:01
7

You can return multiple arrays and scalars from a function

function x()
{
    $a=array("a","b","c");
    $b=array("e","f");
    return array('x',$a,$b);
}

list ($m,$n,$o)=x();

echo $m."\n";
print_r($n);
print_r($o);
zzapper
  • 4,743
  • 5
  • 48
  • 45
7

Its not possible have two return statement. However it doesn't throw error but when function is called you will receive only first return statement value. We can use return of array to get multiple values in return. For Example:

function test($testvar)
{
  // do something
  //just assigning a string for example, we can assign any operation result
  $var1 = "result1";
  $var2 = "result2";
  return array('value1' => $var1, 'value2' => $var2);
}
gsamaras
  • 71,951
  • 46
  • 188
  • 305
Apsar
  • 225
  • 3
  • 6
6

Best Practice is to put your returned variables into array and then use list() to assign array values to variables.

<?php

function add_subt($val1, $val2) {
    $add = $val1 + $val2;
    $subt = $val1 - $val2;

    return array($add, $subt);
}

list($add_result, $subt_result) = add_subt(20, 7);
echo "Add: " . $add_result . '<br />';
echo "Subtract: " . $subt_result . '<br />';

?>
Wael Assaf
  • 1,233
  • 14
  • 24
5

I have implement like this for multiple return value PHP function. be nice with your code. thank you.

 <?php
    function multi_retun($aa)
    {
        return array(1,3,$aa);
    }
    list($one,$two,$three)=multi_retun(55);
    echo $one;
    echo $two;
    echo $three;
    ?>
Ye Htun Z
  • 2,079
  • 4
  • 20
  • 31
5

Yes, you can use an object :-)

But the simplest way is to return an array:

return array('value1', 'value2', 'value3', '...');
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Rufinus
  • 29,200
  • 6
  • 68
  • 84
5

Functions, by definition, only return one value.

However, as you assumed, that value can be an array.

So you can certainly do something like:

<?PHP
function myfunc($a,$b){
   return array('foo'=>$a,'bar'=>$b);
}
print_r(myfunc('baz','bork'));

That said, it's worth taking a moment and thinking about whatever you're trying to solve. While returning a complex result value (like an array, or an object) is perfectly valid, if you're thinking is that "I want to return two values", you might be designing poorly. Without more detail in your question, it's hard to say, but it never hurts to stop and think twice.

timdev
  • 61,857
  • 6
  • 82
  • 92
5

The answer that's given the green tick above is actually incorrect. You can return multiple values in PHP, if you return an array. See the following code for an example:

<?php

function small_numbers()
{
    return array (0, 1, 2);
}

list ($zero, $one, $two) = small_numbers();

This code is actually copied from the following page on PHP's website: http://php.net/manual/en/functions.returning-values.php I've also used the same sort of code many times myself, so can confirm that it's good and that it works.

Sliq
  • 15,937
  • 27
  • 110
  • 143
craig-c
  • 161
  • 2
  • 5
  • That answer referred to the code example in the question, so it is not strictly incorrect. But the question is ambiguous. The intent *is* probably to return two values from one function call. – Peter Mortensen Dec 12 '19 at 21:22
3

Thought I would expand on a few of the responses from above....

class nameCheck{

public $name;

public function __construct(){
    $this->name = $name;
}

function firstName(){
            // If a name has been entered..
    if(!empty($this->name)){
        $name = $this->name;
        $errflag = false;
                    // Return a array with both the name and errflag
        return array($name, $errflag);
            // If its empty..
    }else if(empty($this->name)){
        $errmsg = 'Please enter a name.';
        $errflag = true;
                    // Return both the Error message and Flag
        return array($errmsg, $errflag);
    }
}

}


if($_POST['submit']){

$a = new nameCheck;
$a->name = $_POST['name'];
//  Assign a list of variables from the firstName function
list($name, $err) = $a->firstName();

// Display the values..
echo 'Name: ' . $name;
echo 'Errflag: ' . $err;
}

?>
<form method="post" action="<?php $_SERVER['PHP_SELF']; ?>" >
<input name="name"  />
<input type="submit" name="submit" value="submit" />
</form>

This will give you a input field and a submit button once submitted, if the name input field is empty it will return the error flag and a message. If the name field has a value it will return the value/name and a error flag of 0 for false = no errors. Hope this helps!

Kyle Coots
  • 2,041
  • 1
  • 18
  • 24
3

Yes and no. You can't return more than one variable / object, but as you suggest, you can put them into an array and return that.

There is no limit to the nesting of arrays, so you can just package them up that way to return.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Rick
  • 16,612
  • 34
  • 110
  • 163
3

Functions in PHP can return only one variable. you could use variables with global scope, you can return array, or you can pass variable by reference to the function and than change value,.. but all of that will decrease readability of your code. I would suggest that you look into the classes.

3

Some might prefer returning multiple values as object:

function test() {
    $object = new stdClass();

    $object->x = 'value 1';
    $object->y = 'value 2';

    return $object;
}

And call it like this:

echo test()->x;

Or:

$test = test();
echo $test->y;
hovado
  • 4,474
  • 1
  • 24
  • 30
2

You can always only return one variable which might be an array. But You can change global variables from inside the function. That is most of the time not very good style, but it works. In classes you usually change class varbiables from within functions without returning them.

2ndkauboy
  • 9,302
  • 3
  • 31
  • 65
1

I think eliego has explained the answer clearly. But if you want to return both values, put them into a array and return it.

function test($testvar)
{
  // do something

  return array('var1'=>$var1,'var2'=>$var2);
//defining a key would be better some times   
}

//to access return values

$returned_values = test($testvar);

echo $returned_values['var1'];
echo $returned_values['var2'];
Gihan De Silva
  • 458
  • 8
  • 17
1

The answer is no. When the parser reaches the first return statement, it will direct control back to the calling function - your second return statement will never be executed.

eliego
  • 2,279
  • 2
  • 18
  • 22
1

Add all variables in an array and then finally return the array.

function test($testvar)
{
  // do something
  return array("var1" => $var1, "var2" => @var2);
}

And then

$myTest = test($myTestVar);
//$myTest["var1"] and $myTest["var2"] will be usable
Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175
Ranjan Adhikari
  • 251
  • 1
  • 11
0

Languages which allow multiple returns usually just convert the multiple values into a data structure.

For example, in Python you can return multiple values. However, they're actually just being returned as one tuple.

So you can return multiple values in PHP by just creating a simple array and returning that.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
tplaner
  • 8,363
  • 3
  • 31
  • 47
0

You can get the values of two or more variables by setting them by reference:

function t(&$a, &$b) {
    $a = 1;
    $b = 2;
}


t($a, $b);

echo $a . '  ' . $b;

Output:

1 2
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Jacek Wysocki
  • 1,070
  • 11
  • 24
0
<?php
function foo(){
  $you = 5;
  $me = 10;
  return $you;
  return $me;
}

echo foo();
//output is just 5 alone so we cant get second one it only retuns first one so better go with array


function goo(){
  $you = 5;
  $me = 10;
  return $you_and_me =  array($you,$me);
}

var_dump(goo()); // var_dump result is array(2) { [0]=> int(5) [1]=> int(10) } i think thats fine enough

?>
Liakat Hossain
  • 1,288
  • 1
  • 13
  • 24
-1

Does PHP still use "out parameters"? If so, you can use the syntax to modify one or more of the parameters going in to your function then. You would then be free to use the modified variable after your function returns.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Truck35
  • 19
  • 4
-1
$var1 = 0;
$var2 = 0;

function test($testvar, &$var1 , &$var2)
{
  $var1 = 1;
  $var2 = 2;
  return;
}
test("", $var1, $var2);

// var1 = 1, var2 = 2 

It's not a good way, but I think we can set two variables in a function at the same time.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
khanhkid
  • 94
  • 1
  • 3
-2

This is the easiest way to do it:

public function selectAllUsersByRole($userRole, $selector) {

    $this->userRole = $userLevel;
    $this->selector = $selector;

    $sql = "SELECT * FROM users WHERE role <= ? AND del_stat = 0";
    $stm = $this->connect()->prepare($sql); // Connect function in Dbh connect to database file
    $stm->execute([$this->userRole]); // This is PHP 7. Use array($this->userRole) for PHP 5

    $usersIdArray = array();
    $usersFNameArray = array();
    $usersLNameArray = array();

    if($stm->rowCount()) {
        while($row = $stm->fetch()) {

            array_push($usersIdArray,    $row['id']);
            array_push($usersFNameArray, $row['f_name']);
            array_push($usersLNameArray, $row['l_name']);

            // You can return only $row['id'] or f_name or ...
            // I used the array because it's most used.
        }
    }
    if($this->selector == 1) {
        return $usersIdArray;
    }elseif($this->selector == 2) {
        return $usersFNameArray;
    }elseif($this->selector == 3) {
        return $usersLNameArray;
    }

}

How can we call this function?

$idData = $selectAllUsers->selectAllUsersByLevel($userRole, 0);
print_r($idData);
$idFName = $selectAllUsers->selectAllUsersByLevel($userRole, 1);
print_r($idFname);

That's it. Very easy.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Husam
  • 1
-4

I had a similar problem - so I tried around and googled a bit (finding this thread). After 5 minutes of try and error I found out that you can simply use "AND" to return two (maybe more - not tested yet) in one line of return.

My code:

  function get_id(){
    global $b_id, $f_id;
    // stuff happens
    return $b_id AND $f_id;
  }
  //later in the code:
  get_id();
  var_dump($b_id);
  var_dump($f_id); // tested output by var_dump

it works. I got both the values I expected to get/should get. I hope I could help anybody reading this thread :)

Stefan
  • 17
  • 1
  • 3
    This is not correct. 'AND' is simply a boolean operator, thus your function is actually returning a single boolean value. The only reason it appears to work is because you are declaring $b_id and $f_id as global variables. Remove the 'AND' as well as the return statement altogether and you'll see that your results remain the same. – Matt Styles Feb 19 '15 at 06:34
  • this is very misleading, since you declared $b_id, and $f_id global, therefore available from any context. – paulus Jul 01 '15 at 20:08
-9

use globals like:

<?php

function t($a) 
{
 global $add, $noadd;
 $add=$a+$a;
 $noadd=$a;
}

$a=1;
t($a);
echo $noadd." ".$add;
?>

This will echo 1 2

Degar007
  • 107
  • 1
  • 5