note 22672 deleted from functions.returning-values by googleguy
| From: | googleguy@php.net | Date: | Tue, 22 Oct 2013 12:19:41 +0000 |
| Subject: | note 22672 deleted from functions.returning-values by googleguy | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-197191@lists.php.net to get a copy of this message | ||
Note Submitter: devinemke at yahoo dot com
----
A function can only return one value, but that value can be an array or other compound value. If
you want to just define several variables into the global scope within your function you can do two
things:
1. return an array from your function and then run the extract() function
$result_array = test ();
extract ($result_array);
2. Or you can just append the variables to the $GLOBALS array:
$array = array ('first' => 'john', 'middle' => 'q',
'last' => 'public');
function upper_case () {
global $array;
foreach ($array as $key => $value)
{
$GLOBALS[$key] = strtoupper ($value);
}
}
upper_case ();
echo "$first $middle $last";
// returns JOHN Q PUBLIC
In this second example you can create multiple values without necessarily returning anything from
the function. This may be handy for applying several functions (stripslashes, trim, etc..) accross
all elements of $_POST or $_GET and then having all of the newly cleaned up variables extracted out
for you.