Sunday 4 March 2012

An Introduction to PHP Functions


A function is an input/output “machine”. A function accepts values, processes them and then returns an action.

1. Defining a Function
Functions are defined by using the functions statement :
function someFunction($argument, $argument){

 //function code here

}




2. Calling a Function
Calling a function from within a php script is done with the following syntax :
someFunction($argument, $argument);


3. Returning Values from User-Defined Functions
A variable sent to a function is not actually sent to the function. A copy of the variable is sent to the function and once the function has manipulated that variable it stays local to that function. In other words the variable outside the function stays unchanged. You might need to have access to the result of the function in the form of a variable if, for example you would like to pass the result to another function for manipulation. This is done by using the return statement in conjunction with a variable. The return statement stops execution of the function and returns the value of the variable to the calling code.
function addNums($x, $y) {

 $result = $x + $y;

 return $result;

}

echo addNums(3,5);

//will print 8;

$result

//will contain the result of the function although it will still not be accessible outside the function.


Return can be hard coded, can be the result of an expression or it can be the value returned by another  function call.


4. Variable Scope
As previously mentioned, a variable declared within a function remains local to that function (ie. It will not be available outside that function or in other functions).
function test() {

$testVariable = “this is a test variable”;

}

echo “test variable :”.$testVariable.”<br />”;

//displays error message “undefined variable” as $testVariable is empty outside the function


5. Accessing Variables with the Global Statement
Not only don’t you have access to a variable inside a function from outside the function but also the other way around. Occasionaly you might want to access a variable from within a function without actually passing it in as an argument.
<?php

$var = 0;

function accessingVariable() {

 global $var;

 //some calculation on $var

}


By placing the global statement in front of the variable when it is declared within the function, it now refers the variable ouside the function.
You can declare more than one variable at a time with the global statement simply by separating them with commas.
global $var1, $var2, $var3…


6. Saving State between Function Calls with the Static Statement
The static statement is used to give functions a rudimentary memory.
<?php

function numberOfCalls() {

 static $num_of_calls = 0;

 $num_of_calls++;

}


The variable still remains local to the function but everytime the function is called the variable is remembered and subsequently stored for the next time the function is called.

7. Optional Arguments
By assigning a value to an argument variable within the parentheses of a function, you can make the variable argument variable otional. If the function call doesn’t define an argument then the default value is used instead.

8. Passing Variable References to Functions
When you pass arguments to functions they are stored as copies in parameter variables. Any changes made to them in the body of the function are local to the function. You can change this behaviour by creating a reference to the original variable.
When you pass an argument by reference to a function the contents of the variable you post is accessed by the function’s argument and manipulated within a function, rather than just a copy thereof.
function addFive(&$num) {

 $num += 5;

}

$orignum = 10;

addFive($orignum);

echo $orignum;

//output is 15 as the contents of the variable is manipulated within the function rather a copy thereof.

9. Rules for Calling Functions
Functions need not be defined before they are referenced, except when a function is conditionally defined (eg. Within conditional functions and functions within functions).

No comments:

Post a Comment

Please feel free to comment. No backlinks will be displayed.