Quantcast
Channel: eBookworm» Beginners’ Corner
Viewing all articles
Browse latest Browse all 4

Beginners’ Corner: Variable Function Parameters

$
0
0

Sometimes when defining a function in PHP, you find that there may be cases where you do not want to specify all the possible parameters when calling it. This could be because you want it to use a default value, or that in certain cases it does not logically apply. If you find yourself in such a situation, probably the first thing you should do is ask yourself if two (or more) separate functions should be defined, instead. (Perhaps one function would call the other internally?)

Assuming you want to stick with one function, the most commonly used approach is to define a default value for one or more parameters in the parameter list. A parameter is assigned such a default value via the = operator, just as you would use it to assign a value in the function body.

function foo($bar='World') {
   echo "Hello, " . $bar;
}

When the function is called at run-time, if that parameter is not included in the call, then the default value will be used; otherwise the supplied value is used.

foo(); // Hello, World
foo('Joe'); // Hello, Joe

If you want your function definition to include both required and optional (default) parameters, the required parameters must come first in the list.

function foo($req1, $req2, $opt1='something', $opt2=null) { }

In the preceding example, the use of null can be convenient in that PHP treats a null variable as “not set” by the isset() function, allowing you to do something like:

function foo($bar=null) {
   if(isset($bar)) {
      echo $bar;
   } else {
      echo "Nobody home";
   }
}

Another approach which can be quite powerful in some ways, but for which I’ve really not run into much use myself, is the use of variable length argument lists. Essentially, the function prototype would have no parameters specified, then you use the func_num_args(), func_get_arg(), and/or func_get_args() functions to determine how many arguments were supplied and then decide what to do with them.

function test()
{
   $numArgs = func_num_args();
   switch($numArgs) {
      case 0:
         return false;
         break;
      case 1:
         echo "Hello, " . func_get_arg(0); // args start numbering at zero
         break;
      case 2:
         echo func_get_arg(0) . ", " . func_get_arg(1);
         break;
      default:
         echo "Here are the args:<br /><pre>";
         print_r(func_get_args());
         echo "</pre>";
   }
}

Lastly, another approach is to pass an associative array as the function parameter. You can then test for specific array keys to decide what to do.

function test($args)
{
   if (!is_array($args)) {
      user_error('Oops!');
      return false;
   }
   if (isset($args['name'])) {
      if (isset($args['verb'])) {
         echo $args['verb'] . ", " . $args['name'];
      } else {
         echo "Hello, " . $args['name'];
      }
   }
   return true;
}

A downside of this last method is that the function definition’s parameter list gives no clue by itself as to what array keys are expected/allowed. This can make it tricky to use and maintain, as the coder is at the mercy of the comments and documentation, or else is forced to read the entire function definition to figure out the possibilities.


Viewing all articles
Browse latest Browse all 4

Latest Images

Trending Articles





Latest Images