PHP programming tutorial
5. Functions
Functions are used whenever a common piece of code is needed over and over again throughout a program, or even an entire website. You can define your own functions, perhaps to load in a config file, or display your page’s header. One of the things that makes PHP capable of very rapid development times is the large library of built-in functions. It’s these built-in functions we’re going to look at first.
When you call a function, program flow is transferred into the function, the code within the function is executed and when it’s finished, returns, then program flow continues to the line after the function call.
You might remember this example from chapter 2:
<?php $primes = array(2, 3, 5); $primes[] = 7; $primes[] = 11; var_dump($primes); ?>
Here var_dump is a function call. A function always has a name, optionally has one or more arguments and will often return a value. In this example we’re calling the built-in fuction var_dump with one argument ($primes) and ignoring the return value. If we were to call var_dump with no arguments, we would write the function call var_dump(); – a function call always has brackets. Here’s an example that uses the return value from a function:
<?php $primes = array(2, 3, 5); $primes[] = 7; $primes[] = 11; echo count($primes) . "<br />\n"; ?>
This program calls the built-in function count() with the same array as an argument. The count() function returns an integer, and we’re simply outputting that integer. When you run this program you will get the number 5 followed by an HTML linebreak – this is because the count() function returns the number of items in an array. Notice how we’re using a function call in the same place we could have used a variable, or a static string.
You can also do the following:
<?php $primes = array(2, 3, 5); $c = count($primes); echo $c . "<br />\n"; ?>
In this example we’re assigning the return value of the count() function to the variable $c, and then outputting that variable.
You can also use function calls in conditionals, as in this example:
<?php $primes = array(2, 3, 5); if (count($primes) < 5) { echo "Not enough primes!<br />\n"; } ?>
This will work for loops too – pretty much anywhere you can normally put a variable, you can also put a function call.
When a function has more than one argument, its arguments are separated by commas. substr() is an example of a function that needs more than one argument – here’s a sample program using it:
<?php $str = 'abcdefg'; echo substr($str,0,3); ?>
This will output the string ‘abc’ – substr() returns a sub-section of a string. The first argument is the string itself, the second argument is the start position, with 0 being the beginning of the string, and the third argument is how many characters you’d like.
It is worthwhile to become familiar with PHP’s large library of built-in functions. Documenting all of PHP’s built-in functions is outside the scope of this tutorial and a job best left to PHP’s manual itself. Good places to start are the string functions and the array functions.
How do I define my own functions?
PHP functions can be created using the function keyword followed by the function name, and an optional number of arguments in brackets. Here is an example function definition:
<?php function say_hello() { echo "hello<br />\n"; } say_hello(); ?>
We’re defining a function called say_hello() with no arguments, and that function contains the code to output the word hello. We then call that function so that the code in it is actually executed. When you define a function, the code in it is not actually executed until you actually call the function.
Here’s an example of a function with arguments:
<?php function say_hello_lots($num) { for ($i = 0; $i < $num; $i++) { echo "hello<br />\n"; } } say_hello_lots(10); ?>
This program will output the word hello ten times. We have defined a function called say_hello_lots() that takes one argument – the number of times to repeat the word. We then call that function with an argument of 10.
You can make an argument optional by setting a default value for it like this:
<?php function say_hello_lots($num = 5) { for ($i = 0; $i < $num; $i++) { echo "hello<br />\n"; } } say_hello_lots(); ?>
This program has been modified so that the function sets a default value for its only argument and can therefore be called without any arguments at all – doing so causes the default value to be used and the word hello to be outputted 5 times. You can still call the function with an argument of 10 to get it to loop 10 times.
To make a function return a value use the ‘return’ keyword – here is a very simple example of its usage:
<?php function return_hello() { return "hello"; } echo return_hello(); ?>
Here we are defining a function that returns the string “hello” – we are then calling that function and outputting its return value. If we were to call the function and discard its return value as we have been doing it would have no effect. The following code sample will do absolutely nothing:
<?php function return_hello() { return "hello"; } return_hello(); ?>
If your function mixes arguments both with and without default values, those without default values must come first, followed by the ones with defaults. Here’s an example of a function with multiple arguments:
<?php function nth_char($str, $n = 0) { return substr($str, $n, 1); } $text = "Hello World"; echo nth_char($text) . "<br />\n"; echo nth_char($text,4) . "<br />\n"; ?>
This program will output the letter ‘H’ on a line, followed by the letter ‘o’ on the next line. The nth_char() function returns a single character from a string. The first argument is the string and the second argument is the position within the string where the character should come from. The position has a default value of 0 which is the first letter in the string. The nth_char() function can either be called with one argument or two and examples of both are given.
It is often sensible to put your functions into a separate file from your actual php pages so that you don’t have to keep repeating the same functions in each of the scripts that make up your website. Sometimes it makes sense to have several different files containing libraries of functions – each dedicated to a specific set of tasks. You can include files containing libraries of functions using php’s ‘include’ and ‘require’ statements. These are not functions but built-in constructs – like ‘echo’, you don’t have to use brackets after them (although you can if you want), here’s an example:
In a file called functions.php:
<?php function return_goodbye() { return "goodbye!"; } ?>
In a file called goodbye.php:
<?php include "functions.php" echo return_goodbye(); ?>
