logo
g Text Version
Beauty & Self
Books & Music
Career
Computers
Education
Family
Food & Wine
Health & Fitness
Hobbies & Crafts
Home & Garden
Money
News & Politics
Relationships
Religion & Spirituality
Sports
Travel & Culture
TV & Movies

dailyclick
Bored? Games!
Nutrition
Postcards
Take a Quiz
Rate My Photo

new
Painting
Heart Disease
Horror Literature
Dating
Hiking & Backpacking
SF/Fantasy Books
Healthy Foods


dailyclick
All times in EST

Autism Spectrum Disorders: 4:00 PM

Full Schedule
g
g PHP Site

BellaOnline's PHP Editor

g

The PHP Return Statement

Guest Author - Diane Cipollo

The PHP return statement is used to tell a PHP function to send information back to the calling program. This can be useful in many ways. For example, you can use a function to calculate the sales tax for a shopping cart program. Let's use a factious online business for our example.

This business has an online store and three brick and mortar storefronts in Indiana, Ohio and Maine. This means that sales tax must be calculated for online purchases by customers in these three states. We will use our function to do the calculation. When we call the function, we will need to pass it some information (called arguments). We will pass the total dollar amount of the customer's purchase ($purchases) and the percent sales tax ($sales_tax) for each transaction. We know which state the customer lives in because he gave us that information when he opened his customer account. The function will calculate the amount of sales tax for this purchase and pass that value back to the shopping cart program. Let's take a look at the code. For our example below the total dollar amount of the purchase is $100 and the percent sales tax is .05.

function caltax($purchases, $sales_tax)
{
$result = $purchases * $sales_tax;
return $result;
}

function caltax($purchases, $sales_tax)
The function statement and function name (caltax) begins the function code. Inside the parenthesis, the $purchases variable holds the dollar amount for the transaction ($100) and the $sales_tax variable holds the percentage sales tax (.05). You may be asking why we need to pass the sales tax information each time we call the function. Remember that this company has brick and mortar stores in three states and we need to tell the function which percentage to use each time. If the customer lives in Ohio or Indiana, the percentage would be .06. In Maine, the percentage is .05.

{
Opening curly bracket

$result = $purchases * $sales_tax;
This code will multiply the dollar amount of sales and percent of sales tax. The total amount of sales tax to be collected is $5. This value is stored in the $result variable.

return $result;
The return statement ends the function and sends the value of the $result variable back to the shopping cart program.

}
Closing curly bracket




This site needs an editor - click to learn more!

Add The+PHP+Return+Statement to Twitter Add The+PHP+Return+Statement to Facebook Add The+PHP+Return+Statement to MySpace Add The+PHP+Return+Statement to Del.icio.us Digg The+PHP+Return+Statement Add The+PHP+Return+Statement to Yahoo My Web Add The+PHP+Return+Statement to Google Bookmarks Add The+PHP+Return+Statement to Stumbleupon Add The+PHP+Return+Statement to Reddit




RSS | Editor's Picks Articles | Top Ten Articles | Previous Features | Site Map


For FREE email updates, subscribe to the PHP Newsletter


Past Issues


print
Printer Friendly
bookmark
Bookmark
tell friend
Tell a Friend
forum
Forum
email
Email Editor


Content copyright © 2013 by Diane Cipollo. All rights reserved.
This content was written by Diane Cipollo. If you wish to use this content in any manner, you need written permission. Contact BellaOnline Administration for details.

g


g features
Archives | Site Map

forum
Forum
email
Contact

Past Issues
memberscenter


vote
Poetry
Daily
Weekly
Monthly
Less than Monthly



BellaOnline on Facebook
g


| About BellaOnline | Privacy Policy | Advertising | Become an Editor |
Website copyright © 2013 Minerva WebWorks LLC. All rights reserved.


BellaOnline Editor