tags:

views:

104

answers:

6
function get_tags_by_criteria($gender="%", $min_age_of_birth="%", $max_age_of_birth="%", $country="%", $region="%", $city="%", $tag="") {

when i just want to pass the tag argument and let the others by default, how do i write?

ive tried this but it didnt work.

    get_tags_by_criteria("", "", "", "", "", "", computer);
A: 

It's just not possible in PHP.

Alix Axel
how should i do it then to have the same effect of choosing which argument to pass and which ones i want to use the default value with? i dont want to create 7 different functions for that.
never_had_a_name
I'm sorry but creating 7 different functions is the only option, OOP might help you here though.
Alix Axel
Why was this down-voted?
Alix Axel
+2  A: 

Put the tag argument first:

function get_tags_by_criteria($tag="", $gender="%", $min_age_of_birth="%", $max_age_of_birth="%", $country="%", $region="%", $city="%")

And call it like this:

get_tags_by_criteria(computer);
Justin Ethier
+1  A: 

Unfortunately you can't do this - unspecified optional arguments always have to be at the tail end of a function argument list. Instead, what you can do is use NULL for the arguments you don't wish to specify and then have your function check to see if an argument is NULL and assign it the default value instead.

Amber
Nice answer, null or any other value for that matter.
Alix Axel
Any random value could work, yes, but I tend to prefer NULL since it's unlikely to overlap with any actual non-default value that you might want to pass. :)
Amber
+1  A: 

There is no named parameter passing in PHP. Generally if you have more than about 4 parameters and lots of them are optional you might want to consider using an array or object instead:

function get_tags_by_criteria($args) {
  ...
}

get_tags_by_criteria(array('gender => 'M', 'tag' => 'php'));

You can explicitly set the parameters allowed by using an object instead.

cletus
+2  A: 

You can simulate named arguments using an associative array:

function my_function($options)
 {
  extract($options);
 }

then call

my_function(array("parameter1" => "value1", "parameter2" => "value2"));

that, combined with robust checking and table of default values inside the function, works very nicely for me.

Downside: There is no phpDoc convention to document the arguments, and your IDE will not be able to show the available arguments to you as you type. You will have to enter the available parameters into the @desc block which, depending on your IDE, may or may not look nice.

One workaround for this is to declare all the parameters in the function, but make all of them but the first one optional. The first one can then be the associative array containing the values.

Pekka
You can read more about `extract` here: http://php.net/manual/en/function.extract.php
Steven
A: 

The CakePHP framework often uses associative arrays to specify a set of options. It will even let you specify either individual parameters or an associative array. See the find methods on the model class as an example.

Here's my attempt at making your function more flexible:

<?php
function get_tags_by_criteria(
 $gender = '%', 
 $min_age_of_birth = '%', 
 $max_age_of_birth = '%', 
 $country = '%', 
 $region = '%', 
 $city = '%', 
 $tag = '') 
{
 if (is_array($gender))
 {
  $options = $gender;
  $gender = '%'; // reset to default
  extract($options);
 }

 $msg = "gender=$gender, min_age=$min_age_of_birth, " .
  "max_age=$max_age_of_birth, country=$country, region=$region, " .
  "city=$city, tag=$tag";
 return $msg;
}
?>
<p><?php echo get_tags_by_criteria('M'); ?></p>
<p><?php echo get_tags_by_criteria('M', 10); ?></p>
<p><?php echo get_tags_by_criteria(array(
 'country' => 'ca', 
 'tag' => 'sample')); ?></p>
Don Kirkby