Using Functions


A function is used to make a task that might consist of many lines of code into a routine that can be called using a single instruction.

PHP contains many functions that perform a wide range of useful tasks. Some are built in to the PHP language; others are more specialized and are available only if certain extensions are activated when PHP is installed.

The online PHP manual (www.php.net) is an invaluable reference. As well as documentation for every function in the language, the manual pages are also annotated with user-submitted tips and examples, and you can even submit your own comments if you want.

Online Reference To quickly pull up the PHP manual page for any function, use this shortcut: www.php.net/function_name.


You have already used the date function to generate a string that contains a formatted version of the current date. Let's take a closer look at how that example from Lesson 1, "Getting to Know PHP," works. The example looked like this:

 echo date('j F Y'); 

The online PHP manual gives the prototype for date as follows:

 string date (string format [, int timestamp]) 

This means that date takes a string argument called format and, optionally, the integer timestamp. It returns a string value. This example sends j F Y to the function as the format argument, but timestamp is omitted. The echo command displays the string that is returned.

Prototypes Every function has a prototype that defines how many arguments it takes, the arguments' data types, and what value is returned. Optional arguments are shown in square brackets ([]).


Defining Functions

In addition to the built-in functions, PHP allows you to define your own. There are advantages to using your own function. Not only do you have to type less when the same piece of code has to be executed several times but a custom-defined function also makes your script easier to maintain. If you want to change the way a task is performed, you only need to update the program code oncein the function definitionrather than fix it every place it appears in your script.

Modular Code Grouping tasks into functions is the first step toward modularizing your codesomething that is especially important to keep your scripts manageable as they grow in size and become more complex.


The following is a simple example that shows how a function is defined and used in PHP:

 function add_tax($amount) {   $total = $amount * 1.09;   return $total; } $price = 16.00; echo "Price before tax: $price <br>"; echo "Price after tax: "; echo add_tax($price); 

The function keyword defines a function called add_tax that will execute the code block that follows. The code that makes up a function is always contained in braces. Putting $amount in parentheses after the function name stipulates that add_tax takes a single argument that will be stored in a variable called $amount inside the function.

The first line of the function code is a simple calculation that multiplies $amount by 1.09which is equivalent to adding 9% to that valueand assigns the result to $total. The return keyword is followed by the value that is to be returned when the function is called from within the script.

Running this example produces the following output:

 Price before tax: 16 Price after tax: 17.44 

This is an example of a function that you might use in many places in a web page; for instance, on a page that lists all the products available in an online store, you would call this function once for each item that is displayed to show the after-tax price. If the rate of tax changes, you only need to change the formula in add_tax to alter every price displayed on that page.



    Sams Teach Yourself PHP in 10 Minutes
    Sams Teach Yourself PHP in 10 Minutes
    ISBN: 0672327627
    EAN: 2147483647
    Year: 2005
    Pages: 151
    Authors: Chris Newman

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net