PHP Programing language

adplus-dvertising
How Can Passing variable Numbers of Argument
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");

  1. func_num_args- Returns the number of arguments passed
  2. func_get_arg -Returns a single argument
  3. func_get_args -Returns all arguments in an array

If you want to start the connector function

function connector ()
{
$ arguments = func_get_args();
}
Then you can loop over the arguments like this:

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;
}
Program:
<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