Re: Re: optional argument when creating a function

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Thank you everyone for your help.

I tried this and it worked with throwing any errors.

<CODE>
function doEmail($username, $link = null)
</CODE>


Matthew Weier O'Phinney wrote:

* D A GERM <dgerm@xxxxxxxxxxxx>:
I'm throwing a warning on a function I created. I
thought a & in front of the argument was supposed to
make it optional. Is there something else I need to do
make that argument optional?

As others have noted, the ampersand tells the function to grab a
reference to the variable passed.

There are two ways to do optional arguments:

1) Set a default value for the argument (must be last argument in the
  list, or else all other arguments in the list must also have default
  values):

  function doEmail($username, $link = null) {}
  function doEmail($username, $link = null, $linkName = '') {}

2) Parse the argument list via the func_* functions:

  function doEmail($username)
  {
      $argCount = func_num_args();
      if (1 < $argCount) {
          // Retrieve second argument, from a 0-based array:
          $link = func_get_arg(1);
      }
      ...
  }

  function doEmail($username)
  {
      $argCount = func_num_args();
      if (1 < $argCount) {
          // Retrieve all arguments
          $args = func_get_args();
// Remove $username from it
          array_shift($args);

          // Get $link:
          $link = array_shift($args);
      }
      ...
  }



--
D. Aaron Germ
Scarborough Library, Shepherd University
(304) 876-5423

"Well then what am I supposed to do with all my creative ideas- take a bath and wash myself with them? 'Cause that is what soap is for" (Peter, Family Guy)

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux