Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Optional variables for functions
Old 08-19-2010, 09:26 PM Optional variables for functions
Extreme Talker

Posts: 173
Trades: 0
I am trying to create a standard function that I can use to create text boxes but some times I need more variable then others.

Two questions
1. Is there a limit to the number of variables that can be passed to a function?
2. When you look at the standard library of functions that come with PHP, the have optional values, how can I make values optional?

PHP Code:
textbox_list("Project","input","project",$row['project'],45,45,$tab++) 

PHP Code:
//Creates standard textbox
function textbox_list($label,$type,$name,$value,$maxlength,$size,$tab_index){
 
$id=str_replace(" ","_",strtolower($label));
 if(
$type!='hidden'$string='<li><label for="'.$id.'">'.$label.':</label>';
 else    
$string='<li>'
 
$string.='<input class="text" id="'.$id.'" type="'.$type.'" name="'.$name.'" value="'.$value.'"';
 if (!empty(
$maxlength)) $string.=' maxlength="'.$maxlength.'"';
 if (!empty(
$size)) $string.=' size="'.$size.'"';
 if (!empty(
$tab_index)) $string.=' tabindex="'.$tab_index.'"'
 
$string.=' /></li>';
 return 
$string;

dgkindy is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 08-19-2010, 10:16 PM Re: Optional variables for functions
NullPointer's Avatar
Will Code for Food

Posts: 2,815
Name: Matt
Location: Irvine, CA
Trades: 0
1. I can't say for sure, but there probably is a limit. I'm certain, however, that the limit is higher than the number of parameters you would realistically pass to a function.

2. There are two ways to approach this. You can use default values:
PHP Code:
function add($a$b$c 5)
{
     return 
$a $b $c;
}

echo 
add(11); //will output 7 
or you can dynamically retrieve parameters using func_get_args:
PHP Code:
function add()
{
    
$total 0;
    
$args func_get_args();

    foreach(
$args as $arg)
    {
        if(
is_numeric($arg))
            
$total += $arg;
    }
    return 
$total;
}

echo 
add(123); //will output 6 
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
NullPointer is offline
Reply With Quote
View Public Profile Visit NullPointer's homepage!
 
Old 08-20-2010, 05:46 AM Re: Optional variables for functions
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
Quote:
Is there a limit to the number of variables that can be passed to a function?
Probably, but you could avoid it if ever by passing 1 argument that would be an array or an object.

I often use an object as a return, when I need more than one info given back.
Like a status (true/false) with a explanation (for a login, for example).
Simple to extend to your taste
__________________
Only a biker knows why a dog sticks his head out the window.
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Old 08-20-2010, 02:18 PM Re: Optional variables for functions
NullPointer's Avatar
Will Code for Food

Posts: 2,815
Name: Matt
Location: Irvine, CA
Trades: 0
Quote:
Originally Posted by tripy View Post
I often use an object as a return, when I need more than one info given back.
Like a status (true/false) with a explanation (for a login, for example).
Simple to extend to your taste
I do this as well when I need several pieces of information from a function. If I only need one or two, however, I usually use references:
PHP Code:
function login(&$err)
{
     
/*
     .
     .
     */
     
$err 'Invalid username/password';
     return 
false;
}

if(!
login($errorMessage))
     echo 
$errorMessage
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
NullPointer is offline
Reply With Quote
View Public Profile Visit NullPointer's homepage!
 
Reply     « Reply to Optional variables for functions
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.13743 seconds with 12 queries