Re: user defined function: TextBox()
| From: | CPT John W. Holmes | Date: | Mon, 14 Apr 2003 21:36:46 +0000 |
| Subject: | Re: user defined function: TextBox() | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-143840@lists.php.net to get a copy of this message | ||
> i have a function called TextBox() and its supposed to be used in a form..
> function TextBox($label, $name, $value, $maxsize, $size){
> echo "$label <input type=text name=$name>";
> }
> it is used like this:
> TextBox("Email", "email", ...);
>
> 1. how would i make this function into a variable arguement list
function..
> i want it to take any number of variables or arguements
> 2. giving this function can take any number of arguements how would you
tell
> the function to ignore any variables in the function body to be ignored if
> not used.. i.e. if i have $label and $name used:
> TextBox("Email", "email");
> then how would i have $size $maxsize $value and so on ignored but the
> function work the way its supposed to work...?
Give the optional attributes a default value when you define the function.
Since the "name" is the only required attribute (so you say) then place it
first:
function TextBox($name, $label='', $value='', $maxsize='',
$size=''){
Now you can call the function with just a "name" value and all of the other
variables will be given their default values of an empty string.
Your other option is to use the Function Handling Functions where you can
pass a variable number of values to a function and use those PHP function to
determine how many were passed, what the values were, and act accordingly.
http://www.php.net/manual/en/ref.funchand.php
The first method is the easiest, though, and more along the lines of what
you're looking for.
---John Holmes...