Using Functions Effectivley In PHP

posted by John Owen on July 11th, 2009, in PHP | 4 Comments

Before we start here is a partial definition from wikipedia: A function has an input and an output. The defining property of a function is that, for a given input, there is one and only one output.

Basics

Functions are very simple once you understand them, here is the structure of one:

function () {
 
	}

The PHP code goes inbetween { and }.

Here is an example:

function apple () {
 
		echo 'I have a green apple';
 
	}

As you can see the function name is called apple and when the function is called PHP will output: I have a green apple using echo.

More Complex

Here’s a more complicated example:

function apple () {
 
		// declares the variable apple and sets the text to 'Green Apple'
		$apple = 'Green Apple';
		// returns the variable, which can be used outside the function. Example: $fruit = apple(); echo $fruit;
		return $apple;
 
	}

In this function we declare the variable $apple and set it’s value to ‘Green Apple’, that part is simple but what does the return $apple; bit do? Well, it lets the function have an output. Heres an example to explain better.

<!-- DECLARE FUNCTIONS -->
  <?php 
 
	function apple () {
 
		// declares the variable apple and sets the text to 'Green Apple'
		$apple = 'Green Apple';
		// returns the variable, which can be used outside the function. Example: $fruit = apple(); echo $fruit;
		return $apple;
 
	}
 
	function show_date($show_seconds) {
 
		//if statement, if $show_seconds variable is set to true, the date will also show seconds
		if ($show_seconds == 'TRUE') {
			$date = date("F j, Y, g:i:s a");
		} else {
			$date = date("F j, Y, g:i a");
		}
 
		// return the date either with/without seconds.
		return $date;
}
 
?>
<!-- END FUNCTIONS -->
 
<?php
//declare the fruit variable and set it's value using the return statement in the function, the output should be 'Green Apple'
$fruit = apple();
// print out the text
echo 'I have a ' . $fruit;
?>

This chunk of code may look a little daunting but dont fear, it just has loads of spaces in it. In the declare functions section, I have exactly the same code as before so don’t worry about that. The next piece of code is how you would execute the function.

$fruit = apple();

I declare the variable $fruit and then call the function apple which returns the variable $apple. The $apple variable’s value is ‘Green Apple’. The $fruit variable now has the value of ‘Green Apple’.

echo 'My piece of fruit is a ' . $fruit;

This chunk of code print’s out ‘My piece of fruit is a Green Apple’. The little . between the text and variable add’s the two strings together.

Completing The Function

Now, it’s time to add parameters to the function.

function show_date($show_seconds) {
 
		//if statement, if $show_seconds variable is set to true, the date will also show seconds
		if ($show_seconds == 'TRUE') {
			$date = date("F j, Y, g:i:s a");
		} else {
			$date = date("F j, Y, g:i a");
		}
 
		// return the date either with/without seconds.
		return $date;
}

I’ll explain it step-by-step.

function show_date($show_seconds)

The function is called ’show_date’, inside the brackets I set a variable for the function called $show_seconds.

if ($show_seconds == 'TRUE') {
			$date = date("F j, Y, g:i:s a");
		} else {
			$date = date("F j, Y, g:i a");
		}

This IF statement will check if the $show_seconds variable is equal to the value of ‘TRUE’. If it is, the $date variable will created and it’s value set to the current date including the seconds. If the $show_seconds variable doesn’t equal ‘TRUE’ then it will set the $date variable to the current date without the seconds.

return $date;

This returns the variable of $date, so the function can be used properly. Here is how the function would be used:

echo show_date('TRUE');

OR

echo show_date('FALSE');

OR

echo show_date();

The first example shows the date including the seconds because the value = TRUE, the second example wouldn’t show the seconds because the $show_seconds variable isn’t equal to TRUE and the third example would be the same as the second. It doesn’t equal TRUE.

You can add as much parameters as you like. Here is an example:

function custom_text($text,$html_tag) {
		// compiles a string into the variable custom, if the text was 'Welcome' and the html_tag was bold then it would look like this: <strong>Welcome</strong>
		$custom = '<' . $html_tag . '>' . $text . '</' . $html_tag . '>';
		// outputs the function
		return $custom;
}

If the $text variable was ‘Welcome’ and the variable $html_tag was ’strong’ then the output would look like:

Welcome

General Practice

It is general practice to hold your functions in a seperate file, this way your scripts don’t get clogged down as much. To use the functions in your PHP script, you can use the include or require function. It only needs to be done once and should look something like this:

<?php require_once('functions.php'); ?>

To call the functions from this file you would just call the function name as you would normally.

Finish

Here is a redirect function, no explaining needed anymore ;)

function redirect($url) {
		// redirects without user header to another page. 
		echo "<meta http-equiv='refresh' content='0;url=" . $url . "'>";
}
 
 
/* EXAMPLE FOR CALLING REDIRECT FUNCTION
 
<?php
//redirects to http://www.google.com
redirect('http://www.google.com');
?>
 
*/

Any questions? Other tutorials you want doing? Post a comment! If I got anything wrong or missed anything please feel free to correct me :) .

Download functions.php for the functions in this tutorial!

4 Responses to “Using Functions Effectivley In PHP”

  1. Vince said...

    Thanks for your article. I often forget to use functions and later realise I should have. This helps to understand the basics.

    I would suggest using a functions.php file and require them in your documents.

  2. John Owen said...

    I do usually do that but for the sake of this tutorial I decided to not to. I will probably add it to the tutorial though. Thanks ;)

  3. Marijan Ć uflaj said...

    Functions are very nice, but now classes and object are much more popular and they really make your project a lot easier. You should rather create tutorial about basics on how to work with objects :) .

    An other good way is to use autoload functions to reduce number of include/require statements.

  4. Tobie Kubeck said...

    Very interesting idea, i am seeing here different commands also thanks for sharing. happens all the time

    Follow me on Twitter

Leave a Reply