Previous | Home | Next |
While discussing passing arguments to functions, it's worth nothing that you can set up functions so that they can take a variable number of arguments. In other words, say you have a function named connector, which connects words into a text string. you could call it like this:
Connector ("Hello");
or like this:
Connector (""Hello","there","again");
- func_num_args- Returns the number of arguments passed
- func_get_arg -Returns a single argument
- func_get_args -Returns all arguments in an array
If you want to start the connector function
function connector () { $ arguments = func_get_args(); }
Note that you can get the number of arguments from the func_num_args function:
function connector () { $data=""; $ arguments = func_get_args(); for ($loop_index=0; $loop_index <func_num_args();$loop_index++) { data=$arguments [$loop_index]." "; } echo $data; }
<html> <head> <title>Passing variable arguments to Function </title> <body> <h1>Passing variable arguments to Function </h1> <?php echo "Passing 'How' 'are' 'things?' to connector...<br>"; echo "Getting this result:"; connector ("How","are","things?"); function connector() { $data = ""; $arguments = func_get_args(); for ($loop_index=0; $loop_index <func_num_args(); $loop_index++) { $data .= $arguments [$loop_index]." "; } echo $data; } ?> </body> </html>
Output:

Previous | Home | Next |