|
Using Functions
By
John Dixon
Article Word Count: 331 [View Summary] Comments (0) |
|
Functions are used to perform a well defined task that is normally repeated at various places within a web site, web application, or other software application. The function sometimes needs certain information before performing its task, and sometimes returns a value to the calling page or program.
An example of how to use a simple function in PHP:
function showMessage()
{
echo "{$_SESSION["message"]}";
}
The above example shows a very simple function that displays the contents of a session variable called 'message'. The idea being that while a user is using a web site, etc, various messages are generated and stored in the 'message' session variable. Whenever the showMessage function is called, the contents of the session variable are displayed. So, for example, when the user logs on, you could display a message saying that they have done so. Likewise, when they log off, a different message could be displayed.
Calling a function
To call the showMessage function, you would just need to include the line:
showMessage()
in the web page (wrapped in php tags to distinguish it from normall HTML).
Passing values to a function
Often, a function requires one or more input values in order to perform its task. For example, take the following example that takes two numbers as input, and adds them together. The result is then returned to the calling web page or program.
function addNumbers($number1,$number2)
{
$answer = $number1 + $number2;
return $answer;
}
In the calling program we would have something similar to the following:
$number1 = "5";
$number2 = "6";
$sumOfNumbers = addNumbers($number1,$number2);
echo "$sumOfNumbers";
Obviously, in a real program you would not have the numbers hardcoded like this, but they would be obtained from user input or by some other means. Notice also that although the name of the returned variable is $answer, the calling program makes no reference to that. We could, if we wanted to, change the calling program to:
$answer = addNumbers($number1,$number2);
echo "$answer";
which might make things slightly easier to understand.
|
About the Author: John Dixon is a web developer working through his own company John Dixon Technology Limited The company also develops and supplies a free accounting-bookkeeping software tool called Earnings Tracker. The company's web site contains various articles, tutorials, news feeds, and a finance and business blog. Article Source: http://EzineArticles.com/?expert=John_Dixon |
|
This article has been viewed 96 time(s).
Article Submitted On: February 22, 2008
-
MLA Style Citation:
Dixon, John "Using Functions." Using Functions. 22 Feb. 2008 EzineArticles.com. 23 Nov. 2009 <http://ezinearticles.com/?Using-Functions&id=1002541>.
-
APA Style Citation:
Dixon, J. (2008, February 22). Using Functions. Retrieved November 23, 2009, from http://ezinearticles.com/?Using-Functions&id=1002541
-
Chicago Style Citation:
Dixon, John "Using Functions." Using Functions EzineArticles.com. http://ezinearticles.com/?Using-Functions&id=1002541