Client LoginView your Shopping CartHelp & Frequently Asked QuestionsEmail Us
Home Services Portfolio Web Design Pricing Dreamweaver Templates Web Applications Free Tutorials About Contact Us
Services > CSS Web Design | Professional Logo Design | ASP.net, ASP & PHP Programming | Flash Animation & Programming | Reliable Web Hosting | Website Maintenance
Portfolio > Web Design Portfolio | Web Programming Portfolio | Logo Design Portfolio | Web Design Pricing > Custom Web Design | Logo Design | Business Card Design
Free Web Design Guide > Website Design w/ Dreamweaver & Fireworks | Adobe Fireworks CS4 review | Photo Effects | Text Effects | Graphic Design Ideas | Web Design Ideas
Website Navigation Ideas | CSS Styles Tutorial | Dreamweaver Template | Server Side Includes | Flash Tutorials > Flash Effects | Flash Sound Button | Sound On/Off Button
Flash Preloader | Import External Data | Promotion > SEO Tips | Google Tips & Page Rank | JavaScript > Date Validation | Email Validation | Phone Validation | Tip Boxes

Introduction to Functions in PHP

In this Tutorial you'll learn about Functions in PHP. This includes - What is a Function?, How to define and call functions in PHP.

What is a Function?

A Function is a small set of statements defined by the programmer to do a specific action. They take input values in the form of 'arguments' and return values after execution. They can be written anywhere in the program. They are used to reduce the programming complexity and to handle the programming structures easily. The function takes an input, performs some operation with it and returns a value after successful execution. Functions are basically of two types, namely:

How to define & call functions

Functions are defined using the keyword 'function' followed by the function name. The input parameters are listed in parentheses after the function name, followed by the function code within braces after the arguments as shown below:

Function with no return value

Syntax:

function function_name(parameters)
{
...function code..
return;
}

Example:

<?php
function Tax($salary){
$tax=($salary*33)/100;
echo "Tax for ".$salary." is :".$tax;
return;
}
Tax(3000);
?>

Output: "Tax for 3000 is :990"

In the above example we define a function called 'Tax' with '$salary' as its argument (input). We calculate the tax value using the salary and the output is displayed. Here the function returns no value.

Function with return values

Syntax:

function function_name(parameters)
{
...function code..
return(variable_name);
}

Example:

<?php
function Tax($salary){
$tax=($salary*33)/100;
return($tax);
}
$salary=3000;
$taxamount=Tax($salary);
echo "Tax for ".$salary." is :".$taxamount;
?>

Output: "Tax for 3000 is :990"

In the above example we have redefined the function 'Tax' to return a value. The returned value is assigned to the variable '$taxamount' and the output is displayed as shown above. That's it you've learnt how to use functions in PHP.

Absolutely FREE Web Templates
Check out these quality free web templates and download them without any registration or sign-up!

FREE Web Design Guide
From web design tips & ideas to HTML, CSS Styles, Fireworks & Dreamweaver you'll find all you need to know about effective web site design right here!

Quality Dreamweaver Templates
Professional quality dreamweaver templates in over 20 categories, starting at just $9.95! Instant download & easy customization

Beauty Templates | Business Templates | Christian Templates | CSS Templates | Education Templates | Family Templates | Flash Templates | Free Dreamweaver Templates
Food Templates | General Templates | Government Templates | Health/Medical Templates | Hi-Tech Templates | Kids/Childcare Templates | Low-cost Budget Templates
Personal Templates | Pets Templates | Photography Templates | Profession Templates | Real Estate Templates | Sports Templates | Telecom Templates | Travel Templates
Free Tutorials > Web Design Tutorials | CSS Web Design | Fireworks Tutorials | Dreamweaver Tutorials | Flash Tutorials | SEO Tutorials | Javascript Tutorials | PHP Tutorials