11.07.2015 Views

PHP MySQL - Stilson.net

PHP MySQL - Stilson.net

PHP MySQL - Stilson.net

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

www.it-ebooks.infoCHAPTER 4 • FUNCTIONSOnce 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 threeprerequisite values might be returned from a function using list():list($name, $email, $language) = retrieveUserProfile();echo "Name: $name, email: $email, language: $language";Executing this script returns the following:Name: Jason Gilmore, email: jason@example.com, language: EnglishThis feature is quite useful and will be used repeatedly throughout this book.Recursive FunctionsRecursive functions, or functions that call themselves, offer considerable practical value to theprogrammer and are used to divide an otherwise complex problem into a simple case, reiterating thatcase until the problem is resolved.Practically every introductory recursion example involves factorial computation. Let’s do somethinga tad more practical and create a loan payment calculator. Specifically, the following example usesrecursion to create a payment schedule, telling you the principal and interest amounts required of eachpayment installment to repay the loan. The recursive function, amortizationTable(), is introduced inListing 4-1. It takes as input four arguments: $pNum, which identifies the payment number;$periodicPayment, which carries the total monthly payment; $balance, which indicates the remainingloan balance; and $monthlyInterest, which determines the monthly interest percentage rate. Theseitems are designated or determined in the script listed in Listing 4-2.Listing 4-1. The Payment Calculator Function, amortizationTable()function amortizationTable($pNum, $periodicPayment, $balance, $monthlyInterest){// Calculate payment interest97

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!