Quakenet/#php Tutorial

Note: If you opened this page from an external URL pay attention that all chapters are linked together. Be sure you also read all prior chapters of this tutorial, otherwise you will miss relevant content explained before.

Functions

  1. Usage of functions
  2. Define your own function
  3. Optional parameters
  4. Return values of functions
  5. Documentation of self written function

1. Usage of functions

Besides control structures and variables a php script also use function call. Similar code which is executed often is written in function. Instead of copy&paste the same code all the time you just simply call the function which do the actually work.

The execution of a function can be changed by its parameters. The number of parameters can be everything, even none and infinity. Additionally a function can return a value. This is the normal approach for a function. You call the function with a given number of parameters, the function is doing something and the function returns a value. As an example the sin function returns the sinus of the given radian value.

<?php
sin
(3.1415); // returns float(9.26535896605E-5), which is almost 0
?>

This value can be saved and used like variables.

<?php
$var 
sin(3.1415);
echo 
sin(1.57079); // outputs 0.99999999998
?>

2. Define your own function

Soon or later we want do write a selfwritten php function. A function definition starts with the keyword function.

<?php
function
?>

After that the name is followed. The name must be use the same restrictions like variables as not starting with digits. Additionally function which begins with two underscores (__) are special functions with special meaning. You should use this functions only if you want the effect described in the manual.

<?php
function myFunction
?>

As the same for function calls this peace of code is followed by a opening parenthesis (. Now you can specify variables for the parameters. Each variable is one parameter of the function. If you want a function with any numbers of variables don't write any variables at all and use the functions func_get_args, func_get_arg and func_num_args instead. After the list of parameters the list is finished with the closing parenthesis ).

<?php
function myFunction($param1$param2)
?>

After that the body of the function is followed with the opening curly bracket {. Now you can write every php code you want. The body is closed with the closing curly bracket }.

<?php
function myFunction($param1$param2) {
    
// normal php code
}
?>

It is common to indent the code by one level with 4 spaces. Now you easily can regonize the start of the function (at the keyword function) and where it ends (at the closing curly bracket }).

Inside a function you can use the parameters. Besides the superglobal variables there doesn't exists any other variables from the outer script. This is called variable scope. The parameters are filled with the values of the function call. If these parameters are variables the parameters are filled with copies of these variables. This mean you cannot (basically) change the variables outside of the function.

<?php
function myFunction() {
    echo 
$var;   // help, where is $var defined?
}
$var 'HTML code';
myFunction(); // doesn't work, will say "Undefined variable $var in ... on line ..."
?>

3. Optional parameters

You can define a parameter to be optional. This way you can call a function with less than the number of defined parameters. To define a parameter be optional you add the default value of the parameter at the parameter list. It is done with an assignment. If the function is called with the given parameter the default value is overwrite with the value of the function call otherwise the default values is used.

<?php
function myFunction($x$test 'foobar') {
    echo 
'x got the value: "'.$x.'" and test the value: "'.$test.'"'
}
myFunction(4'word'); // outputs 4 and word
myFunction(5);         // outputs 5 and foobar
myFunction();          // doesn't work as the first parameter is missing
myFunction(123);   // works, but the third value is discarded
?>

4. Return values of functions

As you can use any php code like echo its more common to return a value which is calculated before. To return a value from a function to its caller we use the keyword return. It got a value or variable which should be returned.

<?php
function myFunction() {
    
do_this();  // get's executed
    
return 300;
    
do_that();  // doesn't get executed as the function quits already
}
echo 
myFunction(); // outputs 300
?>

With the return statement the function is automatically finished. This can be done at every place inside the function if it makes sense. If you calculates the sinus of the value 0 you dont need to run any calculation and just return the value 0.

Its possible to quit a function with return without any return value. It makes only sense if the function really don't need to return a value at all.

<?php
function myFunction() {
    
// some code
    
return;
}
myFunction();        // works
$var myFunction(); // works, $var got the value NULL 
?>

5. Documentation of self written function

Functions which are predefined in php are explained on the official homepage. If you write a selfwritten function this explanation is missed of course. You may guess the usage of a function by its name but for complex functions you hope for a well written documentation. For this you can use PHPDocs. Before the function definition you add a PHPDoc comment which explaines the function.

<?php
/**
 * Returns the square of a value.
 *
 * This function is called with one parameter and returns the
 * square of this value.

 * @param x The value for the square value
 * @return The result of the square
 */
function square($x) {
    return 
$x*$x;
}
?>

You should check the phpDocumentor homepage for a detailed explanation.

Questions about the chapter

No questions

Back to Next to
Copyright © to the OPs of #php/QuakeNet Valid XHTML 1.0 Strict Valid CSS!