How to use PHP functions

This brief article shows step-by-step how functions and their parameters are to be used in dynamic web pages coded in PHP.

Why use functions?

Very often you will find yourself using the same code over and over again, especially when using string operations or connecting to databases. Normally you would need to copy and paste the same code several times, and as soon as you wanted to change something, you would need to do that as often as you copied the code. Functions can help :)

Basic function design

A simple function consists of the following elements:

  1. A name, which you could invent yourself (as long as the function does not already exist)
  2. Optionally a parameter, which can be used to call the function
  3. The content of the function, meaning the code to be executed when the function is called
  4. Optionally a return value, if you want the functions call result to be saved within a variable, for example

All that sounds pretty complicated. Well actually it’s pretty simple.

A simple function example

Let’s look at some examples. Let’s say you want your web application to say hello for as often as you like. But you’re not sure wether you want to rename “hello” to “welcome” at some later point in time. So you will first create a function like this:

1
2
3
4
5
<?php
function sayHello() {
echo "Hello";
}
?>

That function can now be called within your script using just one line:

1
2
3
<?php
sayHello(); // will result in "Hello"
?>

The text “Hello” will be presented whenever you call your function that way. If you wanted to change the Hello to something else, you’ll just have to do it at one single code line. That would be your first function.

A parameterized function example

Now let’s say you wanted to greet your logged in user by his name. The problem here is that every user has a different name, right? You can solve this using a parameter:

1
2
3
4
5
<?php
function sayHello($username) {
echo "Hello ".$username;
}
?>

The function now has a parameter called “$username”, which is a variable that can be used to greet your user after saying hello. Pretty simple, right? The function has to be called a different way though:

1
2
3
<?php
sayHello("Mike"); // will result in "Hello Mike"
?>

Of course you would not want to code the name Mike the hard way as presented above. You would just pass the login user name the user entered to that function:

1
2
3
<?php
sayHello($login_name); // will result in "Hello" and the name the user provided after a successful login
?>

A return value function example

Now let’s say it’s not just about greetings, but about calculation. Let’s assume you often need to calculate the following: $a + $b – $c * 5. You can do this using a function like this:

1
2
3
4
5
6
<?php
function calculateMe($a, $b, $c) {
$result = $a + $b - $c * 5;
return $result;
}
?>

The function now expects three parameters: $a, $b, $c, will do the calculation you specified and returns the result to you. You can use that function like this:

1
2
3
<?php
$myResult = calculateMe(1, 2, 3) // the variable "myResult" will carry the value of -12.
?>

Some remarks on functions

Variables you specified within a function are only available within that function — and the other way around. If you want to use a variable which was defined outside the function, use “global $variable_name” within the function. That way you can work with that variable as if it was defined within the function itself.

The other way around, however, needs to be done using the return value as shown in the example above.

These are the basics for working with functions, parameters and return values in PHP. Functions are great. Use them. They will make your life a lot easier :)

Leave a Reply