note 71090 rejected from functions.arguments by nlopess
| From: | nlopess@php.net | Date: | Sat, 11 Nov 2006 20:10:09 +0000 |
| Subject: | note 71090 rejected from functions.arguments by nlopess | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-119614@lists.php.net to get a copy of this message | ||
Note Submitter: exiles at wolfpac dot net
----
It is easier to define a function with optional parameters by defining the function name with blank
arguments and the calling for the arguments within the function itself:
function foo_display ()
{
list ( $name, $address, $phone ) = func_get_args ();
if ( false == empty ( $name ) ):
$arr_return[] = $name;
endif;
if ( false == empty ( $address ) ) :
$arr_return[] = $address;
endif;
if ( false == empty ( $phone ) ):
$arr_return[] = $phone;
endif;
return $arr_return;
}
By this design you can call the function with any number of arguments passed:
$display = foo_display ( "john doe", "minnesota" );
foreach ( $display as $keys ) { print "value: $keys<br>"; }
will output
value: john doe
value: minessota
or:
$display = foo_display ();
foreach ( $display as $keys ) { print "value: $keys<br>"; }
will output a blank line. and there will be no error upon calling the function.
Just a question though, how can I pass an array into the argument list of a function call?
Please advise,
Thanks