Functions are snippets of code that perform specific tasks. Each time a function is run, it can use different inputs (called 'arguments'), so the specific tasks can run more than once with different inputs each time.

PHP has many pre-defined functions (you saw some of them in this lesson already), but programmers can define their own functions as well. Once functions are defined, you invoke (call) them by just stating the function name followed by parentheses ().

Learning all the 3,000 - 4,000 pre-defined PHP functions is beyond the scope of this course. In addition to the ones introduced already in this lesson, you will use the date/time, math and mysql functions frequently.

Here are some examples:

<?php echo "Signed this <b>".date('l')."</b> the <b>".date('jS'); echo "</b> of <b>".date('F')."</b> in the year of our Lord <b>"; echo date('Y')."</b>.<br />"; $mil = 36; $civ = 129; $Mratio = $mil/($mil+$civ); $Cratio = 1 - $Mratio; echo "The audience was ".round($Mratio*100,2)."% military, "; echo round($Cratio*100,2)."% civilian."; ?>

which displays thus:

Signed this Saturday the 13th of July in the year of our Lord 2024.
The audience was 21.82% military, 78.18% civilian.

Note that there are attributes within the parentheses of the functions called above. More about that later.


You can define your own functions. Start with the key word 'function'. Then give your function a name followed by parentheses '()'. Finally, enclose the function coding within curly braces '{}'. Remember, you define the function, but it doesn't execute until you call it. The syntax looks like so:

function functionName() { //code to be performed; }

Here is an example:

<?php function fancyDate() { echo "<span style='font-family:serif; font-style:italic;'>"; echo "This <b>".date('l')."</b> the <b>".date('jS'); echo "</b> of <b>".date('F')."</b> in the year of our Lord <b>"; echo date('Y')."</b>.</span><br />"; } fancyDate(); ?>

which displays thus:

This Saturday the 13th of July in the year of our Lord 2024.

Note how we called the function in order to get the text to display. We can now call this function anywhere we want the fancy date displayed, and we don't have to retype all that code.

You can not define a function more than once. If you defined fancyDate() as you see above, then tried to define it using other code later in the file, PHP won't know which one you want to use. You don't want PHP to overwrite a function you already defined elsewhere since you may not realize you already used that name. Besides, you don't want to inadvertantly name your function some name that was given to a pre-defined function.

Often, you want the function to do something and return a value. The syntax looks like so:

function functionName() { //code to be performed; return $xyz; }

Here is how it is used:

<?php function daysUntilTax() { $today = Mktime(0,0,0); $year = (date('n')<4) ? date('Y') : date('Y')+1; $tax = Mktime(0,0,0,4,15,$year); $daysDiff = floor(($tax-$today)/(60*60*24)); return $daysDiff; } echo "There are ".daysUntilTax()." days until taxes are due!"; ?>

which displays thus:

There are 276 days until taxes are due!

You will frequently need to pass values to a function so the code can be run for those values. The syntax looks like so:

function functionName($arg1, &$arg2, $arg3='default string') { //code to be performed; return $xyz; }

Here is how it is used:

<?php function sigBlock($first,$MI,$last,$rank,&$DoD,$office="Judge Advocate") { $DoD = "USAF"; $sig = "<br /><br /><br /><br /><div style='margin-left:3.5in'>"; $sig .= $first." ".$MI." ".$last.", ".$rank.", ".$DoD."<br />"; $sig .= $office."</div>"; return $sig; } echo "Thanks for all your help!"; echo sigBlock('Frank','T','Jones','Col'); ?>

In the above code, $first, $MI, $last and $rank are all variables defined elsewhere in your code and you want to use them in this function. As you learn from the 'Variable Scope' discussion to follow, whatever you do to these four variables will not affect their values outside the function. (For instance, if you changed $rank in the function to something higher, whatever the function displays will show the higher rank, but the value of $rank will remain as it was for any other time the variable is needed.) $DoD is also a variable defined elsewhere in your code, but since you put the & in front of it, PHP treats it as global in scope, and any changes you make to it within the function will change the variable's value outside the function as well. The last attribute, $office has the default value shown (in this case, "Judge Advocate"). If you define it elsewhere in your code, that value will prevail in the function; if you do not define it anywhere else in your code, the default value will be used.

The above code displays thus:

Thanks for all your help!

Frank T Jones, Col, USAF
Judge Advocate


When variables are defined in your file, they are of global scope. This means their values are available for use almost anywhere in your code. To prevent inadvertant overwriting of existing global variables, any variables defined within a function have a local scope. This means their values are retained only within the function, and not anywhere else. Now you can write a function that has local variables and use it in any of your files without fear of overwriting anything.

Consider the following:

<?php function changeFav($new) { $favorite = $new; echo $favorite."<br />"; } $favorite = "green"; echo $favorite."<br />"; changeFav('blue'); echo $favorite."<br />"; ?>

which displays thus:


We first define $favorite and echo it. It reads as green. Then we call the function changeFav('blue'), which changes the value of $favorite within the function only (local scope) and echo it. Within the function it reads as blue. Then, once we are outside the function, we echo $favorite again. Since we are outside the function (global scope), the value is as it was before. It reads green again.

Loops   < <  PREVIOUS   Table of Contents NEXT  > >   Good Code Practice

Developed with HTML-Kit
Sandersongs Web Tutorials
Contact the Webmasterwith comments.
©2024, by Bill Sanders, all rights reserved.
This domain had 354 different visits in the last 30 days.
This page was last modified on our server on 11 Jul 2021
and last refreshed on our server at 3:46 am, UTC
This file took 0.00576 seconds to process.