Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dealing With Optional Function Parameters 1

Status
Not open for further replies.

Michael42

Programmer
Joined
Oct 8, 2001
Messages
1,454
Location
US
This is the best resource on the web. Thanks all for your help.

My question of the moment is on functions. I want to have a function where the first two parameters are required and all others are optional. Do I need to do anything special so that my function will not blowup if it is missing an optional paramter? I'd rather not use empty parameter place holders ('x','15','','') if possible.

See below to clarify. Thanks!!!

doThis('x','15');

function doThis($a,$b,$c,$d) {
// Will this blow up if it does not get values for $c,$d?
}


 
Code:
function doThis($a, $b, $c="", $d="") {
//Will not blow up, but I should be sure to make sure
//$c and $d aren't blank before I use them
}

-Rob
 
skiflyer,

Very very cool!!!! :-)

This PHP stuff is great.

Thanks,

Michael42


 
No problem, I probably should've noted to that you can fill in default values too, not just blanks. As an illustration...

Code:
function makeTextBox($name, $value="", $size=25)
{
  echo '<input type=&quot;text&quot; name=&quot;'.$name.'&quot; value=&quot;'.$value.'&quot; size=&quot;'.$size.'&quot; />';
}

then later called as...
makeTextBox(&quot;state&quot;);
makeTextBox(&quot;fname&quot;, &quot;rob&quot;, 10);
makeTextBox(&quot;lname&quot;, &quot;&quot;, 30);

-Rob
 
>> I probably should've noted to that you can fill in default values too

Yep I'm there! Very cool. I am finding I write a lot less PHP than I would using, VB (cgi), Perl, ASP or JSP for Web apps.

Thanks again,

Michael42
 
NOTE:
function makeTextBox($name, $value=&quot;25&quot;, $size)

in this case if u use the function like this:
makeTextBox($nm,,$size);
then $value will not be 25. infact it will not allow the above. u have to give it like this:
makeTextBox($nm,&quot;&quot;,$size);



Known is handfull, Unknown is worldfull
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top