Accomplishing
this is much easier than you might think, with the help of a very useful language
construct, list(). The list() construct offers a convenient means for retrieving
values from an array, like so:
$colors = array("red","blue","green");
list($red, $blue, $green) = $colors;
?>
Once the list() construct executes, $red, $blue, and $green will be assigned red,
blue, and green, respectively.
Building on the concept demonstrated in the previous example, you can imagine
how the three prerequisite values might be returned from a function using list():
function retrieveUserProfile()
{
$user[] = "Jason";
$user[] = "jason@example.com";
$user[] = "English";
return $user;
}
CHAPTER 4 ?– FUNCTIONS 121
list($name, $email, $language) = retrieveUserProfile();
echo "Name: $name, email: $email, language: $language";
?>
Executing this script returns the following:
Name: Jason, email: jason@example.com, language: English
This feature is quite useful and will be used repeatedly throughout this book.
Recursive Functions
Recursive functions, or functions that call themselves, offer considerable practical
value to the programmer and are used to divide an otherwise complex problem into
a simple case, reiterating that case until the problem is resolved.
Practically every introductory recursion example involves factorial computation.
Let??™s do something a tad more practical and create a loan payment calculator.
Pages:
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204