[thelist] functions in PHP and arguments

Simon Coggins ppxsjc1 at unix.ccc.nottingham.ac.uk
Fri Aug 31 19:38:04 CDT 2001


Okay so that's input sussed but what about multiple output
variables? Here's an easy way to return multiple variables straight from
php.net:

<tip type="PHP functions" author="Simon Coggins">

Q. How do I return multiple values from a function?

A. You can't return multiple values from a function, but similar results
   can be obtained by returning a list. For example:

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

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

You'll end up with $zero = 0, $one = 1 and $two = 2.

An alternative solution is to create an array within the function and
return the full array:

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

$results = small_numbers();

This time you'll end up with $results[0] = 0, $results[1] = 1 and
$results[2] = 2.

</tip>


Simon






More information about the thelist mailing list