?
Solved

calling function with more parameters

Posted on 2012-08-15
10
Medium Priority
?
432 Views
Last Modified: 2012-08-15
Hi
how i can call a function with more paras
ex:
if i have function A(a,b,c){
....
}
and i will call this function but i have 2 more parameters to pass
it is like i call it with A(a,b,c,d,e)
can you do this or is there another method..
any help please
0
Comment
Question by:asaidi
  • 4
  • 3
  • 3
10 Comments
 
LVL 25

Accepted Solution

by:
lwadwell earned 1000 total points
ID: 38295253
The number of parameters in the call and in the function declaration should match - unless they are optional (i.e. have a default value defined in the declaration.  e.g.
function test_params($a,$b,$c,$d="xyz",$e=123) {
	echo $a, PHP_EOL;
	echo $b, PHP_EOL;
	echo $c, PHP_EOL;
	echo $d, PHP_EOL;
	echo $e, PHP_EOL;
}
//test_params('a','b'); //will fail - not enough parameters
test_params('a','b','c');
test_params('a','b','c','d');
test_params('a','b','c','d',5);

Open in new window

In the above, $d and $e are optional.

Adding more parameters in the call will not change the function if it is not programmed to receive them.

If a variable length list of values is to be passed - pass them as a single array variable with the values in the array elements.
0
 

Author Comment

by:asaidi
ID: 38295269
and $d and $e are then have values in the function not like a,b,c ..
0
 
LVL 60

Assisted Solution

by:Julian Hansen
Julian Hansen earned 1000 total points
ID: 38295450
lwadwell's solution assumes you know the max number of parameters you will need. In the case where you use less the surplus parameters will be given the default values declared in the declaration.

The drawback is though if you want to call with parameter e but not d then you still have to call with a param for d

i.e.
test_param('a','b','c','','e');

And if you want to increase in the future it is a problem.

Another option is to have a single parameter that is an array or object that you can pass a variable number of parameters in for instance
function find_first($table, $params)
{
  $query = "SELECT * FROM $table";
  if (!empty($params['filterl'])) {
    $query .= ' WHERE ' . $params['filter'];
  }
  if (!empty($params['sort'])) {
    $query .= ' ORDER BY ' . $params['sort'];
  }
  if (!empty($params['start'])) {
    $query .= ' LIMIT ' . $params['start'];
    if (!empty($params['total'])) {
      $query .= ',  ' . $params['total'];
    }
  }
  // execute query
}

Open in new window

The above code allows you to pass a variable number of parameters to a function that then builds a query string based on the parameters present.
Example
$params = array(
  'filter' => 'id=9',
  'sort' => 'firstname'
);
find_first('employee', $params);
//OR
$params = array(
  'filter' => 'id=9',
  'sort' => 'firstname',
  'total' => 10,
  'start' => 10
);

find_first('employee', $params);

Open in new window

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 25

Expert Comment

by:lwadwell
ID: 38295584
>> and $d and $e are then have values in the function not like a,b,c ..
I do not understand.

$a, $b, $c, $d and $e ALL have values in the function.  For $a, $b, $c - they are the passed values in the call of the function.
test_params(         'a','b','c','d',     5); // calls the function passing values as parms
// The passed parms   |   |   |   |       |
// are related by     |   |   |   |       |
// location in list:  V   V   V   V       V 
function test_params($a, $b, $c, $d="xyz",$e=123) { // receives the parms
	....

Open in new window


This is a rigid structure.  If you need flexibility ... as I mentioned and @julianH demonstrated - use arrays as the passed parameter.
0
 

Author Comment

by:asaidi
ID: 38295627
Hi
can i pass this
 p5($date,$date2,$report,$ser,$client=$client,$netw=$netw);
instead of d='xyz' and e=123
i put $client receives $client it is a varibale and the same for the netw
0
 
LVL 25

Expert Comment

by:lwadwell
ID: 38295661
If the variable is defined in the functions definition ... i.e.
   function p5 ($v1, $v2, $v3, $v4, $v5)
yes ... $client will be $v4 in the function and $netw will be $v5.

If you called it:
   p5($date,$date2,$report,$ser,$client=$client,$netw=$netw, $another);
it will work OK ... but the extra parameter will be ignored by the function.
0
 
LVL 60

Expert Comment

by:Julian Hansen
ID: 38295663
I think you are confused between a declaration

function p5($a, $b, $c, $d='123', $e='abc');

and calling the function

$value = 1;
p5('1', $value, 'blue');

With the above when the p5 function is entered

$a => '1'
$b => 1
$c => 'blue'
$d =>'123' // takes on the default value for the parameter because no parameter was passed
$e=>'abc'  // takes on the default value for the parameter because no parameter was

can you give more information on what you are trying to do - you are asking questions about parameters but if we know more about what you want to achieve we might be able to answer the question better.
0
 

Author Comment

by:asaidi
ID: 38295679
ok
i have a form that has
customer list it is a scrolling list
net unit :list scroll of water units
date1
date2
if the user leave blank customer report will print all the customers for a unit
my function will be p5($netw,$date1,$date2)
another scenario the user can choose a customer and a unit
my function will be
include('report.php');
p5($client,$netw,$date1,$date2)
i want to write one program that prints total and etc..not for each scenario i create a program
means p5,p6,p7..
i hope you understand me
i need one and can hold the params send by user
0
 
LVL 60

Expert Comment

by:Julian Hansen
ID: 38295731
Then take a look at my earlier post html#a38295450

That is one of dealing with this alternatively declare your function as lwadwell demonstrated above
p5($client, $netw, $date1, $date2)

And pass null values or empty strings for the values you don't want the function to deal with.

Either way you are going to have to put some logic in the p5 function to test each of the parameters and output based on whether they have a value or not.
0
 
LVL 25

Expert Comment

by:lwadwell
ID: 38295780
Firstly - to be very clear about this - the order you pass the variables matters.  Going from
  p5($netw,$date1,$date2)
to
  p5($client,$netw,$date1,$date2)
where the new parameter comes first will not work.  If you want optional parameters ... they should go last.  e.g.
  p5($netw,$date1,$date2,$client)

Hence the function could be setup to default the $client value, e.g.
  function p5($v1, $v2, $v3, $v4="ALL CLIENTS") { ...

Then in the function have logic that handles when $v4 == "ALL CLIENTS" differently to when a value is passed in the function call.

Or you could bundle them all into an array and pass the array.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses
Course of the Month15 days, 10 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question