Content of the lesson:
You can improve the structure you program using functions. A programmer often gets into situation when he wants to repeat a sequence of commands (for example the connection to database from the previous lesson, displaying records from database or writing the header part of page).
If you use these commands repeatedly at different places it is not a good idea to repeat the whole construction. In case you needed any change, you would have to change every sequence of commands manually.
We will use functions to be able to place the sequence of commands inside it and then only call the function from our program.
We used functions in the previous lessons (for example the function MySQL_Connect or fopen). In this case we talk about standard functions which are a part of PHP. However, this lesson will show how to create your own functions.
Before describing the definition of function, you should know the general notation:
function name(parameter1, parameter2, ...) { commands; return value; }
As you can see from the notation, every function has to begin with the keyword function. The name of function follows this keyword - you will call the function using this name from your PHP script. AFTER the name of function you should add brackets which contain input parameters - single parameters are separated by commas.
There are braces in the following lines and all commands have to be placed inside these braces. One of the commands is the command return (written in the penultimate line). This command is used to return the return value (the result) of this function. When working with simple functions you do not have to write this command. You also do not have to enter any input parameters - you can leave empty brackets instead of them.
function pozdrav() { echo "Welcome to my WWW page!"; }
Then you only have to call the function from the main program. You can call it repeatedly but you have to know that a function has to be defined before calling it (the best place is at the beginning of the script).
pozdrav();
In this part we will focus on input parameters of a function which are inserted into the brackets. You can insert input parameters to any function to be able to use them inside it. Such a parameter will act as a variable inside that function.
Why do we have to create parameters and cannot use variables which were created outside the function? Variables are not global and for example a variable $hodnota which is created somewhere in your PHP script can be different than another variable $hodnota which is used inside a function. All data which has to be processed inside a function has to be inserted using input parameters.
We will return back to our function from the previous sample. We will adjust the function to output the text "Good morning" before 12 AM and the text "Good afternoon" after 12 AM. To simplify the solution we will insert a parameter to our function which will contain the number of hours from the current time.
function pozdrav($hodina) { if ($hodina < 12) { echo "Good morning!"; } else { echo "Good afternoon!"; } }
According to adjustments in the definition of our function we also have to adjust the call to it:
$cas = 13; pozdrav($cas);
The only part of function, which was mentioned at the beginning but has not been used yet, is the return value. The return value can be passed using the command return which should be followed by a value or a name of variable which contains the required value.
The return value returns the result of any function which can be later processed by a parent script or by the function which called our function. You can achieve this by assigning the function into a variable - we did this in the previous lesson when we saved the state of connection to database into the variable $spojeni using the command $spojeni = MySQL_Connect("localhost","root","");.
Usage of the return value is shown in the following example of function which can be used to compute the volume of a cuboid.
function objem($a, $b, $c) { $objem = $a*$b*$c; return $objem; }
The function objem requires 3 input parameters. These parameters are used like variables $a, $b and $c inside the function. After launching this function a local variable $objem is created and the volume is saved into it. Then this value is returned as the result of this function using the keyword return.
Using the command return is necessary because you cannot access the variable $objem outside this function (it is a local variable as mentioned in the previous part). This variable expires when the function is ended.
$height = 10; $width = 20; $depth = 5; echo "The volume of this cuboid is ".objem($height , $width , $depth );
Finally, you can see another example which returns to the previous lesson where the procedure of connecting to database was explained. Because you will use the connection to database very often, you can create a function which will connect to your database and use this function in almost every page of your website.
In case you adjust the example from the previous lesson you will get the following result:
function dbconn() { $spojeni = MySQL_Connect("localhost","root",""); if (!$spojeni) { die('Could not connect: ' . mysql_error()); } else { echo 'Connected successfully'; mysql_query("SET CHARACTER SET utf8"); mysql_select_db("renome"); return $spojeni; } }
The last problem which should be solved is using the same function in more PHP files. Inserting the function at the beginning of each file is not a good idea because you would have to change all files in case you wanted to change the function.
You can solve this problem by placing your function into a separate file which can be named as utils.php and this file will be referred in all PHP scripts which will use that function. For this purpose you can use the function require which has one input parameter - the path to file which should be inserted.
<?php function dbconn() { $spojeni = MySQL_Connect("localhost","root",""); if (!$spojeni) { die('Could not connect: ' . mysql_error()); } else { echo 'Connected successfully'; mysql_query("SET CHARACTER SET utf8"); mysql_select_db("renome"); return $spojeni; } } ?>
<?php require("utils.php"); ... $spojeni = dbconn(); ... ?>