15 February 06
Beginner PHP Tutorial 7: Creating and calling functions
The functions PHP offers are powerful and can be used effectively, but you can create your own functions which are closer to your needs and split your code up to improve readability. Functions complete a task and are passed parameters. Some take no parameters at all whereas some take many. Functions also output a single value, which could be a boolean, a string, an integer or any other type which is in php. I could go on and on forever about the importance of using functions and classes (don’t worry, that’s later when we talk about OOP) in your scripts, but this tutorial is about the how and not the why. You can find plenty of articles on the web of why they are useful.
<?php
function add($num_a, $num_b){
return $num_a + $num_b;
}
?>
In this case if you call add(2, 4) the result will be 6 (the two numbers added together. The return value can be sent to a function:
<?php
echo add(2, 4);
?>
...or captured into a variable:
<?php
$added = add(2, 4);
?>
Well, that’s great you say, but how did you actually create the function. There is a function definition (when you are creating a function) format:
function FUNCTION_NAME(PARAMS){ CODE }
...and you can use return (which is special like echo) to return a value. FUNCTION_NAME is the name of your function and PARAMS are the values being handed to it. When defining a function you use a variable name like $blah and then within the function you are given access to that variable and its value.
However, when calling a function you can use a variable, a value or a constant and the value will be sent through to the function. Then, within the function’s scope (the global scope is outside) the variables defined in PARAMS will be created and given the appropriate values according to their order. So calling add(4, 2) made $num_a be equal to 4 and $num_b be equal to 2. Understand?
A variable defined in the function code is not available in the global scope, so basically you cannot use a variable you set in a function when outside that function or in another run of the function or a different function. So all function scope variables are destroyed after the function is run except the return value which is handed back to the script. This means the first call to your function might set $a to equal 4 but on the second run, within the script or in another function this will be forgotten.
Well, it’s not that limited, some keywords are provided to assist. These are global and static.
Global
In a function you can define some variables to be brought from the global scope (the script) into the function scope and taken back into the global scope after the function is complete. So the function test() is called and it defines $a as global (using global $a;). It then gives $a a value of 2 and returns true (using return true;). After the function is called $a is printed on the screen (using echo $a;). Magically the number 2 is shown. If you remove the global statement the number 2 will disappear. Here is the code for you to test:
<?php
function test(){
global $a;
$a = 2;
return true;
}
echo $a;
?>
...and without the global statement:
<?php
function test(){
$a = 2;
return true;
}
echo $a;
?>
You can make multiple variables global in only one call by separating them by commas like so:
global $a, $b;
The global statement is useful but try to minimize the amount you use it in your scripts. If you need the global statement lots you need to continue on these tutorials up to the Advanced section where I explain PHP 4 and PHP 5 classes.
Static
This is an amazing statement, but you probably won’t use it all that much. Let’s have a technical definition from php.net: “A static variable exists only in a local function scope, but it does not lose its value when program execution leaves this scope.”. So after the script ends the value isn’t lost. Wait, this is the same as the global statement! No it isn’t. It isn’t transferred to or from the global scope and remains in the function scope. So if you call the function again the variable will still be there but in other functions or in the script it will be gone.
Only on the first run is the static statement executed and the variable defined. On runs after that the variable simply holds the value. Let’s play around with this:
<?php
function test(){
static $a = 0;
$a++;
echo $a;
}
test();
?>
This will make the function run once and output ‘1’. If you call the function more times it will not output ‘1’ again though, it should output a number 1 larger than the previous. Try this:
<?php
function test(){
static $a = 0;
$a++;
echo $a;
}
for($i=0; $i<9; $i++){
test();
}
?>
The output should be ‘123456789’. Impressed?
Calling your function
So you have defined a function, now let’s call it. Our function add() accepts two parameters: $num_a and $num_b and outputs a result. So we call:
<?php
add(4,2);
?>
...but nothing is outputted. 4 + 2 is simply calculated and then the value is forgotten, what a waste of CPU eh! Let’s output the value:
<?php
echo add(4,2);
?>
...and it is as simple as that. You can now call your function, no problem!

