index act articles resource 22 ext iframe

Creating User Defined Function

This article shows you how to create your own functions.




Creating User Defined Function
1150 Views
Introduction


Functions can be defined as a reusable code. Technically speaking, functions return a value, whereas a procedure is invoked by a procedure call statement. By default, PHP has hundreds of built in functions. For example, if() is a PHP function along with for() and hundreds more. A simple look at the php manual will show you the hundreds of functions available.




User defined functions


If you wish to reuse a piece of code throughout your script, it might be worth creating a function for it. To create a PHP function, you must follow the following criteria:

  • A function defintion will start like: function function_name(optional parameters)

  • The function name must start with a letter or an underscore and must be easy to understand and should not be the same as an existing function.

  • After determining the function name and optional parameters (more to come on parameters) you start the function by an open brace ( { ) and finish the function with a closing brace ( } ).

Setting own function


The below code, shows a very simple function named display_name.

Code: [hide]
  1. function display_name()
     
  2. {
     
  3.    echo ‘DooBDee’;
     
  4. } 

To access this function is simple, simply type the following : display_name();. Using display_name() anywhere in your script will now always display the output of the defined function.




Adding Parameters


To add a parameter to a function (a value which is passed to a function) simply change the opening line of the function definition and change the function code. For example a modification of the above function with one parameter:

Code: [hide]
  1. function display_name($name)
     
  2. {
     
  3.    echo ‘Hello, my name is: ‘, $name;
     
  4. } 

To access this variable, simply use: display_name(‘DooBDee’);This function can now be reused for many different names.




Additionally, you can add more than one parameter to a function. To add an extra parameter, simply separate variable names using a comma in the opening function definition line like so:

Code: [hide]
  1. function display_name($name, $address)
     
  2. {
     
  3.    echo ‘Hello, my name is: ‘, $name, ‘ and I live at: ‘, $address;
     
  4. } 

The variable names in the function must match the function names in the first line of the function defintion otherwise they will not be able to be accessed. To use the above function, you would simply use something like: display_name(‘DooBDee’, ‘a computer’);. This could also be used anywhere throughout your script where the function is included, otherwise you would get an error like so:

Code: [hide]
  1. Fatal error: Call to undefined function: display_name() 

Returning a value


Technically, we have been previously talking about a procedure. A function is truely a set of code which returns a value. They are setup in the exact same way except they use a keyword called return to return a value back to the script from the function. For example:

Code: [hide]
  1. function display_sum($num, $num2)
     
  2. {
     
  3.    return $num + $num2;
     
  4. } 

To use this function, you would simply use something like: echo ‘3 + 4 = ‘, display_sum(3, 4);. Functions can return many data types including, but not limited to booleans, strings and integers.




Conclusion


To conclude, functions are reusable code which can return output or just execute a procedure of code.

Creating User Defined Function