Functions are a fundamental part of programming and often called methods or routines. There are two types of function: one that returns a value and one that simply executes and returns nothing. You generally can add a function anywhere in your PHP code and they are global so you can put them in include files to keep your code tidy. Take this simple example:

<?

function printHello()

{

echo ‘Hello Word’;

}

printHello();

?>

In this example the function is read into memory. It is only called when the program/server runs the printHello(); line then it will jump to the start of the function run the code inside and jump back to the printHello() line once it hits the close bracket. You could have put printHello() above the function and it still would have worked. How can functions help? This example is probably a waste of time to write but what if we wanted to print a list of names from a database every time a page loaded. Do we add the many lines of code manually to every page or do we create one global function and use a simple call like printNames()?

Functions come into their own when they return a value.

<?
$c = 12;

//calls addThis function
$add = addThis(3,7);

// prints 10 on screen
echo $add;

function addThis($a,$b)
{
$total = $a + $b;
return $total;
}
?>

You will notice that this function has $a and $b between brackets. These are called parameters and the $a and $b now take the values you sent the function when you called it (3 and 7). The function adds them up and sends the $total back. This value is stored in the variable $add.

Please note that all variables in a function are local and functions by default cannot see variables outside the function and vice versa. This function would not be able to see $c variable and once the function ends all the local variables within it are effectively destroyed ($a, $b, $total).
To allow a function to see a variable outside its scope you must tell it to by using the global keyword.

<?

$c = 12;
$total = 4;

//calls addThis function
$add = addThis(3,7);

// prints 22 on screen
echo $add;

function addThis($a,$b)
{
global $c;
$total = $a + $b + $c;
return $total;
}
?>

I’ve added $total variable outside the function. This variable would not be effected in any way by the $total variable in the function as they are in different scopes. It would still remain as 4.

PHP has many built in functions that return values and one of the main ones you’ll use is the date() function. The date function has many formats and it can return different values depending on what you put in. Unfortunately you cannot overload a function in PHP but in true polymorphism style there are different ways to achieve the same results. If you don’t know what overloading is, it doesn’t matter too much.
With the date() function we can see the true power of the function:
(examples taken from php.net)

<?
// set the default timezone to use. Available since PHP 5.1
date_default_timezone_set(‘UTC’);

// Prints something like: Monday
echo date(“l”);

// Prints something like: Monday 8th of August 2005 03:12:46 PM
echo date(‘l jS \of F Y h:i:s A’);

// Prints: July 1, 2000 is on a Saturday
echo “July 1, 2000 is on a ” . date(“l”, mktime(0, 0, 0, 7, 1, 2000));

/* use the constants in the format parameter */
// prints something like: Mon, 15 Aug 2005 15:12:46 UTC
echo date(DATE_RFC822);

// prints something like: 2000-07-01T00:00:00+00:00
echo date(DATE_ATOM, mktime(0, 0, 0, 7, 1, 2000));
?>


<?
// Assuming today is March 10th, 2001, 5:16:18 pm, and that we are in the
// Mountain Standard Time (MST) Time Zone

$today = date(“F j, Y, g:i a”); // March 10, 2001, 5:16 pm
$today = date(“m.d.y”); // 03.10.01
$today = date(“j, n, Y”); // 10, 3, 2001
$today = date(“Ymd”); // 20010310
$today = date(‘h-i-s, j-m-y, it is w Day’); // 05-16-18, 10-03-01, 1631 1618 6 Satpm01
$today = date(‘\i\t \i\s \t\h\e jS \d\a\y.’); // it is the 10th day.
$today = date(“D M j G:i:s T Y”); // Sat Mar 10 17:16:18 MST 2001
$today = date(‘H:m:s \m \i\s\ \m\o\n\t\h’); // 17:03:18 m is month
$today = date(“H:i:s”); // 17:16:18
?>

Leave a Reply

You must be logged in to post a comment.