note 22672 added to functions.returning-values

From: Date: Wed, 26 Jun 2002 15:45:45 +0000
Subject: note 22672 added to functions.returning-values
Groups: php.notes 
Request: Send a blank email to php-notes+get-32226@lists.php.net to get a copy of this message
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. -- http://www.php.net/manual/en/functions.returning-values.php http://master.php.net/manage/user-notes.php?action=edit+22672 http://master.php.net/manage/user-notes.php?action=delete+22672 http://master.php.net/manage/user-notes.php?action=reject+22672

« previous php.notes (#32226) next »