Friday, 2010-07-30

Who's Online

We have 7 guests online


Breadcrumbs

Home Function Function


Function

                                    PHP Functions

                     

 The real power of PHP comes from its functions.

 In PHP, there are more than 700 built-in functions.

 

A function is just a name we give to a block of code that can be executed whenever we need it. This might not seem like that big of an idea, but believe me, when you understand and use functions you will be able to save a ton of time and write code that is much more readable!

  • PHP Functions


In this chapter we will show you how to create your own functions.

To keep the browser from executing a script when the page loads, you can put your script into a function.

A function will be executed by a call to the function.

You may call a function from anywhere within a page.

 

Tip: Although functions are often thought of as an advanced topic for beginning programmers to learn, if you take it slow and stick with it, functions can be just minor speedbump in your programming career. So don't give up if you functions confuse you at first!

 

  • Create a PHP Function


A function will be executed by a call to the function.

Simply tell PHP we're talking about a function, give it a name, follow it with ( ) (that's the part the gets closer to rocket science -- more on that later) and specify the instructions it contains.

Syntax:

                                       function functionName()
                                                   {
                                                     code to be executed;
                                                   }

 

  • Give the function a name that reflects what the function does
  • The function name can start with a letter or underscore (not a number)

Example:

A simple function that writes my name when it is called:

Code:

                                       <html>
                                       <body>

                                       <?php
                                                 function writeName()
                                                            {
                                                              echo "Google Chrome";
                                                            }

                                                            echo "My favorite browser is ";
                                                            writeName();
                                        ?>

                                        </body>
                                        </html>

 

An output will be:

                                   My favorite browser is Google Chrome

 

Here we've defined a function called "writeName".  Function names are case sensitive, meaning that writeName is not the same as writename.  Personally, I like to capitalize each word in the name so that it is easier to read.  The instructions contained within a function are not processed (executed) until the function is called, regardless or where within your program the function is defined.  This means that theoretically, your functions could be defined anywhere within the program.  Best practice, however, is to define all your functions at the beginning of your program script. You'll thank yourself later.

Note: Your function name can start with a letter or underscore "_", but not a number!

 

  • PHP Functions - Adding parameters


To add more functionality to a function, we can add parameters. A parameter is just like a variable.

Parameters are specified after the function name, inside the parentheses.

Another useful thing about functions is that you can send them information that the function can then use. Our first function writeName isn't all that useful because all it does, and ever will do, is print out a single, unchanging string.

However, if we were to use parameters, then we would be able to add some extra functionality! A parameter appears with the parentheses "( )" and looks just like a normal PHP variable.

 

Example:

The following example will write different first names, but equal last name:

Code:

                                          <html>
                                          <body>

                                          <?php
                                                   function writeName($fname)
                                                               {
                                                                  echo $fname . " Hendrix.<br />";
                                                               }

                                                               echo "My name is ";
                                                                       writeName("Jimmy");
                                                               echo "My sister's name is ";
                                                                       writeName("Lara");
                                                               echo "My brother's name is ";
                                                                       writeName("Vince");
                                          ?>

                                          </body>
                                          </html>             

 

An output will be:

                                    My name is Jimmy Hendrix.
                                    My sister's name is Lara Hendrix.
                                    My brother's name is Vince Hendrix.

 

It is also possible to have multiple parameters in a function. To separate multiple parameters PHP uses a comma ",". Let's modify our function to also include ages of every members:

Code:

                                           <html>
                                           <body>

                                           <?php
                                                     function writeName($fname,$punctuation)
                                                                  {
                                                                     echo $fname . " Hendrix" . $punctuation . "<br />";
                                                                  }

                                                                  echo "My name is ";
                                                                           writeName("Jimmy",".");
                                                                  echo "My sister's name is ";
                                                                           writeName("Lara","!");
                                                                  echo "My brother's name is ";
                                                                           writeName("Vince","?");
                                           ?>

                                           </body>
                                           </html>

 

An output will be:

                                      My name is Kai Jimmy Hendrix.
                                      My sister's name is Lara Hendrix!
                                      My brother's name is Vince Hendrix?

 

  • PHP Functions - Return values


Besides being able to pass functions information, you can also have them return a value. However, a function can only return one thing, although that thing can be any integer, float, array, string, etc. that you choose!

How does it return a value though? Well, when the function is used and finishes executing, it sort of changes from being a function name into being a value. To capture this value you can set a variable equal to the function. 

 

Example:

To let a function return a value, use the return statement.

Code:

                                           <html>
                                           <body>

                                           <?php
                                                     function add($x,$y)
                                                                 {
                                                                   $total=$x+$y;
                                                                   return $total;
                                                                  }

                                                                  echo "1 + 16 = " . add(1,16);
                                           ?>

                                           </body>
                                           </html>

 

An output will be:

                                        1 + 16 = 17

 

Remember: When you are creating a function, follow these simple guidelines:

  • Always start your function with the keyword function
  • Remember that your function's code must be between the "{" and the "}"
  • When you are using your function, be sure you spell the function name correctly
  • Don't give up!

 

We will add more reference about PHP function in another chapter but for now hope that this was usefull to understand function.

For a complete reference and examples of the built-in functions, please visit: www.php.net

I personally recomand to read all tutorial before learning more about function. 

 

 

    PHP FORM