2

I am having situation where i want to pass variables in php function. The number of arguments are indefinite. I have to pass in the function without using the array. Just like normal approach. Comma Separated.

like test(argum1,argum2,argum3.....,..,,.,.,.....); How i will call the function? Suppose i have an array array(1,2,3,4,5) containing 5 parameters. i want to call the function like func(1,2,3,4,5) . But the question is that, How i will run the loop of arguments , When calling the function. I tried func(implode(',',array)); But it is taking all return string as a one parameters

  • In the definition, I also want the same format.

  • I can pass variable number of arguments via array but i have to pass comma separated.

  • I have to pass comma separated. But at the time of passing i don't know the number of arguments , They are in array.

4
  • 4
    RTLM: php.net/manual/en/… Commented Jun 1, 2013 at 7:15
  • Yes , I have gone through this, but my question is how to pass the variables? if i don't know the number of arguments, if they are in indefinite array. Commented Jun 1, 2013 at 7:19
  • you can simply pass the by separating with comma. Commented Jun 1, 2013 at 7:24
  • How, I will pass separating by comma. at run time .. Commented Jun 1, 2013 at 7:26

6 Answers 6

3

At the calling side, use call_user_func_array.
Inside the function, use func_get_args.

Since this way you're just turning an array into arguments into an array, I doubt the wisdom of this though. Either function is fine if you have an unknown number of parameters either when calling or receiving. If it's dynamic on both ends, why not just pass the array directly?!

Sign up to request clarification or add additional context in comments.

2 Comments

Wow :) Thanks. This is what i am searchingfor call_user_func_array
I am creating a PHP framework and for the ease of user i am putting the parameters in function it self :)
2

you can use :

$function_args = func_get_args();

inside your test() function definition .

Comments

2

You can just define your function as

function test ()

then use the func_get_args function in php.

Then you can deal with the arguments as an array.

Example

function reverseConcat(){
      return implode (" ", array_reverse(func_get_args()));
}

echo reverseConcat("World", "Hello"); // echos Hello World

If you truely want to deal with them as though they where named parameters you could do something like this.

function getDistance(){
     $params = array("x1", "y1", "x2", "y2");
     $args = func_get_args();
     // trim excess params
     if (count($args) >  count($params) {
         $args = array_slice(0, count($params));
     } elseif (count($args) < count($params)){
         // define missing parameters as empty string
         $args = array_pad($args, count($params), "");
     } 
     extract (array_combine($params, $args));
     return sqrt(pow(abs($x1-$x2),2) + pow(abs($y1-$y2),2));

}

2 Comments

Thanks @Orangepill, But , How i will call the function? Suppose i have an array array(1,2,3,4,5) containing 5 parameters. i want to call the function like func(1,2,3,4,5) . But the question is that, How i will run the loop of arguments , When calling the function. I tried func(implode(',',array)); But it is taking all return string as a one parameters..
The way you do that is to use call_user_func_array("reverseConcat", $array);
0

use this function:

  function test() {
      $args = func_get_args();
      foreach ($args as $arg) {
        echo "Arg: $arg\n";
      }
    }

Comments

0

I'm not sure what you mean by "same format." Do you mean same type, like they all have to be a string? Or do you mean they need to all have to meet some criteria, like if it's a list of phone numbers they need to be (ddd) ddd-dddd?

If it's the latter, you'll have just as much trouble with pre-defined arguments, so I'll assume you mean you want them all to be the same type.

So, going off of the already suggested solution of using func_get_args(), I would also apply array_filter() to ensure the type:

function set_names() {

    function string_only($arg) {

        return(is_string($arg));

    }

    $names_provided = func_get_args();  
    // Now you have an array of the args provided

    $names_provided_clean = array_filter($names_provided, "string_only"); 
    // This pulls out any non-string args

    $names = array_values($names_provided_clean); 
    // Because array_filter doesn't reindex, this will reset numbering for array.

     foreach($names as $name) {
           echo $name;
           echo PHP_EOL;
      }
}

set_names("Joe", "Bill", 45, array(1,2,3), "Jane");

Notice that I don't do any deeper sanity-checks, so there could be issues if no values are passed in, etc.

Comments

-2

You can use array also using explode http://www.php.net/manual/en/function.explode.php.

$separator = ",";
$prepareArray = explode ( $separator , '$argum1,$argum2,$argum3');

but be careful, $argum1,$argum2, etc should not contain , in value. You can overcome this by adding any separator. $separator = "VeryUniqueSeparator"; I don't have code so can't tell exact code. But manipulating this will work as your requirements.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.