PHP functions

This page is marked as In Progress so expect small errors or unfinished bits

If you are used to other programming languages you are probably used to functions (or methods, procedures etc.). These are chunks of code which are normally placed at the top of a program or script and are then "called" from further down in the program. PHP scripts may not use functions as much as some other languages but they are there.

Built-in functions

PHP has a large library of built-in functions which your scripts can use. PRINT_R and STR_REPLACE are just two examples. Others are listed on php.net although it can be hard to find the one you need.

User-defined functions

You can also write your own as you may have done with JavaScript functions. To do this just place the function before any place it might be called:

function fred() {
	echo "Hello";
}
        

Then later in the code you can call the function any time you want it to echo "Hello":

fred();
        

Of course it would be easier to just ECHO hello where you need it but with more complicated code it is easier to place it away from the flow of your script.

Passing data/variables

Just as with built-in functions you can pass data to functions. To extend the above example:

function fred($wordtosay) {
	echo $wordtosay;
}
        

You now need to call the function by including the word to say:

fred("Hello");
        

You can pass more than one piece of data by separating them by commas (so the function becomes:

function fred($wordtosay, $name) {
	echo $wordtosay." ".$name;
}
        

or, perhaps more simply:

function fred($wordtosay, $name) {
	echo "$wordtosay $name";
}
        

and the function is called like this:

fred("Hello", "John");
        

To pass numbers just leave off the quotes.

You can also pass variables to the new function:

fred($something);
        

Global and static variables

You can skip this section and use the method described above unless you know other programming languages. If you know other programming languages you may make an incorrect assumption about PHP so read this now!

In most programming languages a global variable is declared outside of any functions. That makes it available to all code including functions. In PHP global variables are available to all functions but the functions will not use them unless you tell them to. Look at this example:

$fred="hi";

function thisfunction() {
	echo $fred;
}
       	

If you know other programming languages you would expect this to ECHO the word hi. It does not. This is a safety precaution to stop people writing functions which overwrite existing global variables. In most languages if you accidentally use a variable name which exists globally you will change the value of the global variable and cause problems. In contrast PHP will create a local variable unless you tell it to use the global variable:

$fred="hi";

function thisfunction() {
	global $fred;
	echo $fred;
}
       	

That will ECHO hi.

You can also declare a variable as static. These variables are only created/declared once even if the function is called more than once. The first time the function is called the variable is created but as it then exists it is not re-created next time the function is used. Instead the value already in it is preserved.

It may be easier to pass values to functions rather than using either global or static variables.

INCLUDE and INCLUDE_ONCE

If you have lots of functions or one very long one it can make the page cluttered. You may also want to use the function on many pages. To do that put the function (or functions) in a page on it's own and save it (you could call it myfunctions.php). Then include that code in any page by using:

include("myfunctions.php");
        

Then call the function as normal. INCLUDE effectively places the function in the current page as if you had typed it in there. Typically put includes at the top of the page. You can also include HTML pages or just chunks of PHP which you want on every page (without using functions).

As projects get larger you may accidentally include a file more than once which slows things down. It is safer to get into the habit of using:

include_once("myfunctions.php");
        

If that file has already been included nothing will be done. Both work fine so make a choice.

submit to reddit Delicious Tweet