PRE-DEFINED FUNCTIONS
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:
which displays thus:
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.
USER DEFINED FUNCTIONS
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:
which displays thus:
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:
which displays thus:
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:
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:
Judge Advocate
VARIABLE SCOPE
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:
which displays thus:
blue
green
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.