Jump to content

Pass all variables in function


-Zeus-

Recommended Posts

Hi,

 

I'm new to PHP, so bear with me if this is common knowledge.  I did a search and could find no pertinent information.

 

I currently have a script run by <?=shell_exec('script.sh arg1 arg2 arg3')?>, and i want to change it to a function, script(arg1 arg2 arg3).  However, everything about functions I have seen so far requires that you name the variables in the header.  I am looking for something independant of the number of variables - e.g.,

 

function script($*)

{

  echo shell_exec('script.sh $*');

}

 

Is this possible?  I think it must be.

Link to comment
https://forums.phpfreaks.com/topic/149475-pass-all-variables-in-function/
Share on other sites

function someFunc () {
   // example 1:
   $count = func_num_args();
   for ($x = 0; $x < $count; $x++) {
      echo "Argument $x : " . func_get_arg($x) . "<br/>";
   }

   // example 2:
   $args = func_get_args();
   foreach ($args as $n => $v) {
      echo "Argument $n : $v";
   }
}

someFunc();
someFunc(100);
someFunc('abc',123);
someFunc(1,true,"omg",'x',1234,$blah);

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.