Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Prodentim Probiotics Specially Designed For The Health Of Your Teeth And Gums

Instant Grammar Checker - Correct all grammar errors and enhance your writing

Teach Your Child To Read

Powerful WordPress hosting for WordPress professionals

Disclosure: My content contains affiliate links.

31,166 questions

40,722 answers

573 users

How to multiple returns from a function in PHP

8 Answers

0 votes
function multiple_returns()
{
    $var1 = "1";
    $var2 = "2";
    $var3 = "3";

    return array($var1, $var2, $var3);
}

echo multiple_returns()[0];
echo "<br />";
echo multiple_returns()[1];
echo "<br />";
echo multiple_returns()[2];
echo "<br />";

/*
run:

1
2
3

*/







answered May 17, 2015 by avibootz
0 votes
function multiple_returns()
{
    $var1 = "1";
    $var2 = "2";
    $var3 = "3";

    return array($var1, $var2, $var3);
}

list($a, $b, $c) = multiple_returns();
echo $a;
echo "<br />";
echo $b;
echo "<br />";
echo $c;
echo "<br />";

/*
run:

1
2
3

*/




answered May 17, 2015 by avibootz
0 votes
function multiple_returns()
{
    return array(1, 2, 3);
}

list($a, $b, $c) = multiple_returns();
echo $a;
echo "<br />";
echo $b;
echo "<br />";
echo $c;
echo "<br />";

/*
run:

1
2
3

*/




answered May 17, 2015 by avibootz
0 votes
function multiple_returns()
{
    return array(1, 2, 3);
}

echo multiple_returns()[0];
echo "<br />";
echo multiple_returns()[1];
echo "<br />";
echo multiple_returns()[2];
echo "<br />";

/*
run:

1
2
3

*/




answered May 17, 2015 by avibootz
0 votes
function multiple_returns(&$a, &$b, &$c)
{
    $a = 1;
    $b = 2;
    $c = 3;
}

multiple_returns($a, $b, $c);

echo $a;
echo "<br />";
echo $b;
echo "<br />";
echo $c;
echo "<br />";

/*
run:

1
2
3

*/




answered May 17, 2015 by avibootz
0 votes
function multiple_returns() 
{
    return array('a' => 1,
                 'b' => 2,
                 'c' => 3);
}

$array = multiple_returns();

$a = $array['a'];
$b = $array['b'];
$c = $array['c'];

echo $a;
echo "
"; echo $b; echo "
"; echo $c; echo "
"; /* run: 1 2 3 */




answered May 17, 2015 by avibootz
0 votes
function multiple_returns() 
{
    return array(1, 2, 3);
}

$array = multiple_returns();

$a = $array[0];
$b = $array[1];
$c = $array[2];

echo $a;
echo "
"; echo $b; echo "
"; echo $c; echo "
"; /* run: 1 2 3 */




answered May 17, 2015 by avibootz
0 votes
class ABC
{
    public $a;
    public $b;
    public $c;
}

function multiple_returns()
{
    $obj = new ABC();

    $obj->a = 1;
    $obj->b = 2;
    $obj->c = 3;

    return $obj;
}

$returned_obj = multiple_returns();

$a = $returned_obj->a;
$b = $returned_obj->b;
$c = $returned_obj->c;

echo $a;
echo "<br />";
echo $b;
echo "<br />";
echo $c;
echo "<br />";

/*
run:

1
2
3

*/




answered May 17, 2015 by avibootz
...