A function is something that performs a specific task. People write functions if they plan on doing the same task over and over again. This allows you to only write the code once and save a lot of time and space.
Although in the next few pages we will lean how to write our own functions, PHP has several functions that already exist for us to use. Although they all have different uses, all functions are phrased as: name(argument). The name being the name of the function, and the argument being the value(s) it is using.
Here are some examples of functions already in PHP:
$a = abs(-.43);
$b = sqrt(16);
$c = round(12.3);
print "The absolute value of -.43 is " . $a . "
";
print "The square root of 16 is " . $b . "
";
print "12.3 rounded is " . $c . " and 12.5 rounded is " . round(12.5);
?>
This gives an example of three functions; absolute value, square root, and rounding. As you can see you can use the function right in the print statement or you can assign it to a variable. A list of functions can PHP has the ability to dynamically generate the time and date. Using a simple line of code we are able to include this on our site, however it is important to know how the formatting works.
The above code outputs a long string of numbers. What these numbers represent is the time based in the amount of seconds that have passed since January 1 1970 00:00:00 GMT. This number can also be assigned to a variable:
$b = time ();
print $b;
?>
Although this is a handy feature, sometimes you want a more formatted and human friendly representation of the date. You can use the date function in conjunction with the time function to display this in the format of date ( format , time ) In our case we want the start time to be now, so we will call the time first. We will demonstrate many different types of formatting
$b = time ();
print date("m/d/y",$b) . "
";
print date("D, F jS",$b) . "
";
print date("l, F jS Y",$b) . "
";
print date("g:i A",$b) . "
";
print date("r",$b) . "
";
print date("g:i:s A D, F jS Y",$b) . "
";
?>
When you run this code you will see that the information is formatted in many different ways. What each of the letters means for formatting is explained on the next page.
Date Function Formatting
As you can see in our last example there are tons of different formats that can be used in the date feature. Below is a summary of the variable used in date, and what each does. Remember they ARE CaSe sEnsItIVe:
DAYS
d - day of the month 2 digits (01-31)
j - day of the month (1-31)
D - 3 letter day (Mon - Sun)
l - full name of day (Monday - Sunday)
N - 1=Monday, 2=Tuesday, etc (1-7)
S - suffix for date (st, nd, rd)
w - 0=Sunday, 1=Monday (0-6)
z - day of the year (1=365)
WEEK
W - week of the year (1-52)
MONTH
F - Full name of month (January - December)
m - 2 digit month number (01-12)
n - month number (1-12)
M - 3 letter month (Jan - Dec)
t - Days in the month (28-31)
YEAR
L - leap year (0 no, 1 yes)
o - ISO-8601 year number (Ex. 1979, 2006)
Y - four digit year (Ex. 1979, 2006)
y - two digit year (Ex. 79, 06)
TIME
a - am or pm
A - AM or PM
B - Swatch Internet time (000 - 999)
g - 12 hour (1-12)
G - 24 hour c (0-23)
h - 2 digit 12 hour (01-12)
H - 2 digit 24 hour (00-23)
i - 2 digit minutes (00-59)
s 0 2 digit seconds (00-59)
OTHER e - timezone (Ex: GMT, CST)
I - daylight savings (1=yes, 0=no)
O - offset GMT (Ex: 0200)
Z - offset in seconds (-43200 - 43200)
r - full RFC 2822 formatted date
Writing Functions
While having pre-made functions is useful, for most things you are going to want the flexibility to write your own custom functions. A function you create takes this shape: function functionname () { your code }
Below is an example of two simple functions:
function examplefunction ()
{
print "Hi, I'm a Function
";
}
function sqr( $num )
{
$NumSqr = $num * $num;
return $NumSqr;
}
Print "Sample Line 1
";
examplefunction();
Print "Sample Line 3
";
$a = 9;
$b = sqr( $a );
Print $a . "^2 = " . $b;
?>
As you can see above, the code we defined between the { and } is all executed when we call the function below. In our first example (examplefunction) we used the function to simply print text. In our second function (sqr) we needed an operand to complete the math within the function. We put this inside the parenthesis () after the function name. We then add in the RETURN line, so that the value is outputted to the spot in the code where we originally called the function.
Functions can contain any of the information we have learned so far. Let's combine functions with some of the material we learned earlier to create a multiplication table:
function mul()
{
global $start;
print "
for ($num=1; $num <= 10; $num++ )
{
$cell = $num * $start;
print "
}
print "
}
$start = 0;
print "
?>
One new thing you may have noticed is "GLOBAL". Since the variable $start is not defined within the function, we use the tag "GLOBAL" to let it know that it needs to use the $start variable that we have defined outside of the function.

Comments
0 comments to " "
Posting Komentar