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

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

new
Journals
Folklore and Mythology
Business Coach
Marriage
Senior Living
Ethnic Beauty
Adolescence


dailyclick
All times in EST

Full Schedule
g
g PHP Site
Theresa Applegate
BellaOnline's PHP Editor

g

How To Set A Default Value For An Argument Variable In A PHP Function
Guest Author - Diane Cipollo

In this tutorial, you will learn how to use a default value for an argument variable in a PHP function. This default value is used by the function only if there is no value for the argument variable passed to the function in the function call.

In the previous tutorials, you learned how to write a basic PHP function and you learned how to pass information to the function via the function call. Let's take a look at a simple function and function call.

Basic PHP Function and Function Call
function Hello_Visitor($current_name)
{ echo "Welcome, $current_name"; }

Hello_Visitor("Nancy");

As you can see, this Hello_Visitor function has one argument variable, $current_name. In the function call the value passed to the function for the $current_name variable is Nancy. But, as you can imagine, if there is no value passed in the function call, the only thing printed on the web page will be Welcome,. So, to prepare for situations when no value is passed to the function, you can set a default value to be used. Let's set the default value to everyone. Then, when the default value is used, the message sent to the web browser will be Welcome, everyone. Here is the altered code that will set a default value for the argument variable.

function Hello_Visitor($current_name = "everyone")
{ echo "Welcome, $current_name"; }

Hello_Visitor( );

function function_name($argument = value)
function Hello_Visitor($current_value = "everyone")
As you can see, a default value is set by assigning the value to the argument variable within the parenthesis. ($argument = value) As stated above, the value everyone will be used if no other value is passed via the function call.

function_name( );
Hello_Visitor( );
In this basic function call there is nothing between the parentheses and therefore no value is passed to the function.





RSS | Previous Features | Site Map

Add How+To+Set+A+Default+Value+For+An+Argument+Variable+In+A+PHP+Function+ to Twitter Add How+To+Set+A+Default+Value+For+An+Argument+Variable+In+A+PHP+Function+ to Facebook Add How+To+Set+A+Default+Value+For+An+Argument+Variable+In+A+PHP+Function+ to MySpace Add How+To+Set+A+Default+Value+For+An+Argument+Variable+In+A+PHP+Function+ to Del.icio.us Digg How+To+Set+A+Default+Value+For+An+Argument+Variable+In+A+PHP+Function+ Add How+To+Set+A+Default+Value+For+An+Argument+Variable+In+A+PHP+Function+ to Yahoo My Web Add How+To+Set+A+Default+Value+For+An+Argument+Variable+In+A+PHP+Function+ to Google Bookmarks Add How+To+Set+A+Default+Value+For+An+Argument+Variable+In+A+PHP+Function+ to Stumbleupon Add How+To+Set+A+Default+Value+For+An+Argument+Variable+In+A+PHP+Function+ to Reddit


Content copyright © 2009 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 Theresa Applegate for details.

g


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

g features
Archives | Site Map

forum
Forum
email
Contact

Past Issues
memberscenter

jobs
what
job title, keywords
where
city, state or zip
jobs by job search


vote
Growing a Garden
Veggies and Flowers
Veggies Only
Flowers Only
No Garden

g


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


BellaOnline Editor