Thorough PHP Functions Tutorial

What Can Functions Do For You

Functions server as a way to represent many lines of code in one line under one name. If there is a snippet of code that you use through out your site wrapping it in a function will give you the advantage of having to change it only in one place if changes need to be made.

Declaring and Naming Functions

Just like variables, functions cannot begin with numbers or symbols except the underscore. The naming conventions for functions and variables are the same, the only difference is that functions do not need the dollar sign in front every time you use them.

Before you use a function however it has to be declared.

Declaring a function is easy:

  • Type the keyword function in front of the function’s name
  • Follow the name by a set of opening and closing parenthesis.
  • You will also need an opening and closing curly bracket.
// example
function my_function()
{
 // body of function, do something here
}

The keyword function and function name is called the function’s header, in my previous example the header was function my_function().

The function’s body is all the statements inside the curly brackets. This is the part of the function that gets executed whenever you call the function.

If you were to execute the previous function nothing would happen, this is because the only code inside the function’s body is a comment.

A Function That Displays Something

Let’s go ahead and make a function that displays the text “Welcome”. Am going to call this function “greeting” .

function greeting()
{
 echo "Welcome"
}

To call a function all you need is to type its header. Running the next snippet will display the word "Welcome"

greeting();

You can call the function as many times as you like in the same script.

Functions With Parameters or Arguments

Our greeting function does what we need, but it’s not dynamic. A better function would be one that displays the word “Welcome” followed by a user’s name. This is where function parameters come in handy.

Parameters or arguments are variables that you can pass to a function. These variables are defined inside the function’s parentheses, our greeting function has no parameters yet so let’s pass it a variable called “name” which  you will be able to use inside the function’s body.

function greeting($name)
{
 echo "Welcome ".$name;
}

Great! Your function can now greet users based on their name.

Executing functions with parameters work the same way as executing functions without them. The only difference is of course, that you need to pass it the parameters.

greeting("Matt"); // will display "Welcome Matt"

Your parameter can also be a variable and it can, but it does not have to, have the same name as in the function declaration.

$user='Matt';
$name='Sabrina';
greeting($user); // will display "Welcome Matt"
greeting($name); // will display "Welcome Sabrina"

Functions With More Than One Argument

Sometimes there will be more than one value that can vary inside a function’s body, this means that you function should be able to take more than one parameter. Parameters are separated by commas.

Let’s make a function that can add three numbers.

function adder($x,$y,$z)
{
  echo $x+$y+$z;
}

If run the following line the result would be six.

adder(1,2,3); // will display 6

If you have a long list of parameters you might consider using an array instead. Let’s redo the adder function, this time using an array.

function adder($num)
{
  echo $num[1]+$num[2]+$num[3];
}

$numbers[1]=4;
$numbers[2]=6;
$numbers[3]=8;

adder($numbers); // Will display the number 18

Functions That Return Values

So far you’ve only seen functions that display text inside the function, most of the times though you will need to get a value with a function but send that value to another function before displaying it.

Echo is a function itself, so let’s make a function that calculates the addition of two numbers but displays them out side of the function. The new keyword you will have to learn is return

function adder($x,$y)
{
  // add them
 $result=$x+$y;

   return  $result; // return ends the function call
}

If you ran the line

adder(4,5);

nothing would happen, that is because we never told PHP to display the result, just return it. Let’s go ahead a show the result.

echo adder(4,5); // will display 9

A Function With Multiple Returns

A function can have as many return statements as you like but only one will get executed, everything after it will be ignored.

Make a function that adds or subtracts two numbers based on a third parameter.

function arithmetic($x,$y,$operation)
{
   if($operation=='+')
   return $x+$y;
   return $x-$y;
}

echo arithmetic(7,7,'+'); // will display 14
echo arithmetic(7,7,'-'); // will display 0

How does this work? Like I said, only one return can get executed, everything after will be ignored. Note that our arithmetic function is not perfect though,

echo arithmetic(7,7,'*'); // will also result in 0

Functions That Return Themselves: Recursive Functions

One really cool trick about functions is that you can have them return themselves, I really can’t think of a good example of this type of functions because I haven’t had to use one so am going to show you the classic example.

If you wanted to calculate a factorial, that is a number that times itself minus 1 until it gets to one you could go with a while loop, or you could use a recursive function.

Example: The mathematical notation 4! (4 factorial) is equal to 4*3*2*1 which will result in 24.

This can easily be done with a recursive function.

function factorial($num)
{
	if($num==1)
	return $num;
	return $num*factorial($num-1); // here the function returns itself
}

echo factorial(4); // will display 24

Functions are the first step for getting into object oriented programming which I will write about in my next posts, so stay tuned and don’t forget to share this article and subscribe 😉