Prev | Current Page 195 | Next

W. Jason Gilmore

"Beginning PHP and MySQL: From Novice to Professional"

00 at 5.75 percent interest. For reasons of space conservation,
just the first 12 payment iterations are listed.
124 CHAPTER 4 ?–  F UNCT I ONS
Figure 4-1. Sample output from amortize.php
Function Libraries
Great programmers are lazy, and lazy programmers think in terms of reusability.
Functions offer a great way to reuse code and are often collectively assembled into
libraries and subsequently repeatedly reused within similar applications. PHP libraries
are created via the simple aggregation of function definitions in a single file, like this:
function localTax($grossIncome, $taxRate) {
// function body here
}
function stateTax($grossIncome, $taxRate, $age) {
// function body here
}
function medicare($grossIncome, $medicareRate) {
// function body here
}
?>
Save this library, preferably using a naming convention that will clearly denote its
purpose, such as taxes.library.php. Do not however save this file within the server
document root using an extension that would cause the Web server to pass the file
contents unparsed. Doing so opens up the possibility for a user to call the file from the
CHAPTER 4 ?–  FUNCTIONS 125
browser and review the code, which could contain sensitive data. You can insert this
file into scripts using include(), include_once(), require(), or require_once(), each of
which is introduced in Chapter 3. (Alternatively, you could use PHP??™s auto_prepend
configuration directive to automate the task of file insertion for you.


Pages:
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207