EzineArticles - Expert Authors Sharing Their Best Original Articles



  Submit Articles
  Members Login
  Benefits
  Expert Authors
  Read Endorsements
  Editorial Guidelines
  Author TOS

  Terms of Service
  Ezines / Email Alerts
  Manage Subscriptions
  EzineArticles RSS

  Blog
  Forums
  About Us
  What's New
  Contact Us
  Article Writing Shop
  Advertising
  Affiliates
  Privacy Policy
  Site Map


Advanced Search


Would you like to be notified when a new article is added to the Web-Development category?

Email Address:


Your Name:


Prefer RSS?
Subscribe to the
Web-Development
RSS Feed:

Using Functions
Print This Article Ezine Publisher Send To Friends Add To Favorites Post A Comment Suggest Topic Report Author

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

John Dixon - EzineArticles Expert Author

Other Recent EzineArticles from the Internet-and-Businesses-Online:Web-Development Category:

Most Viewed EzineArticles in the Internet-and-Businesses-Online:Web-Development Category (90 Days)

  1. How to Create a Web Page Shadow Using Photoshop Slice Tool and HTML Coding
  2. Find Out the Best Free Website Creator
  3. Make Your Own Websites - What You Should Know
  4. Choosing Java Vs .Net For Web Development
  5. How to Put Hyperlink in an Image to Create a Clickable Picture!
  6. Under Construction Website - Create an Under Construction Web Page to Protect Yourself !
  7. How Much Does a Website Cost - An Inside Look From a Web Designer
  8. Creating Web Pages With PHP, CMS, and Joomla
  9. How to Create an HTML Image Background For Your Webpage
  10. Start My Own Website - Make Millions While I Sleep
  11. Build My Own Website in 2 Easy Steps
  12. How to Write Specifications For a Website
  13. Add a Flash Event Calendar to Your Website
  14. The Best Free Website Builder Service on the Internet
  15. Understanding the Basics of Owning Your Own Website

Most Published EzineArticles in the Internet-and-Businesses-Online:Web-Development Category

  1. Make a Website Free - 5 Benefits of Building a Website For Your Online Business
  2. 4 Reasons Why Its Important to Have a Website For Your Business - Or You Will Not Make Any Money
  3. Understanding the Basics of Owning Your Own Website
  4. How to Build a Website - Know More About It
  5. Make Your Own Website the Quick and Easy Way - Without Sacrificing Quality Or Options
  6. Make Money Online - Landing Page Creation
  7. How to Design a Social Networking Website
  8. Marketing Your Website Online
  9. Web Development Estimation
  10. Top 6 Website Basics You Must Know
  11. Under Construction Website - Without an Under Construction Web Page You Are in DANGER!
  12. Photography Website Music and Why it Hurts Your Business
  13. Creating Your Website - What You Need to Do to Make it LIVE!
  14. How to Build a Small Business Website For Less Than $100
  15. Photography Website How to For the Professional Photographer

 

This article has been viewed 96 time(s).
Article Submitted On: February 22, 2008



© EzineArticles.com - All Rights Reserved Worldwide.