|
After you have created a function that does something useful, you will probably want to use it again in other scripts. Rather than copy the function definition into each script that needs to use it, you can use a library file so that your function needs to be stored and maintained in only one place. Before you go any further, you should create a library file called tax.php that contains both the add_tax and add_tax_rate functions but no other PHP code.
Including Library FilesTo incorporate an external library file into another script, you use the include keyword. The following includes tax.php so that add_tax can be called in that script: include "tax.php"; $price = 95; echo "Price before tax: $price <br>"; echo "Price after tax: "; echo add_tax($price);
You can use the include_once keyword if you want to make sure that a library file is loaded only once. If a script attempts to define the same function a second time, an error will result. Using include_once helps to avoid this, particularly when files are being included from other library files. It is often useful to have a library file that includes several other files, each containing a few functions, rather than one huge library file.
|
|