RE: Re: blank function parameters

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

 



To view the terms under which this email is distributed, please go to http://disclaimer.leedsmet.ac.uk/email.htm



On 03 November 2004 16:39, Matthew Weier O'Phinney wrote:

> * Daniel Schierbeck <dasch@xxxxxxxxxx>:
> > Giles Roadnight wrote:
> > > If I defined a function with 4 parameters but only pass 3 I get an
> > > error. Is there anyway around this?
> > > 
> > > I want to be able to set the missing parameter to a default value
> > > if it is not passed which works ok but How do I get rid of the
> > > error message? 
> > 
> > If you want an argument to be optional and still be able to check
> > whether or not it's been set, you can use NULL as the default value:
> > 
> > function foobar ($a, $b, $c = null)
> > {
> >      if (isset($c)) {
> >          echo 'The third argument was set';
> >      }
> > }
> 
> That check should be for 'is_null($c)' as the default value of $c will
> be null, and it will be always set, even if not sent.

Nope.  At least not necessarily.  isset(NULL) returns true, so the test as
originally written is valid.  However, it may be that you want to handle the
no-value-supplied case first, in which case:

    if (is_null($c)):
        /// handle default case
    else:
       ...
    endif;

might be slightly more legible than

    if (!isset($c)):

etc.

Cheers!

Mike

---------------------------------------------------------------------
Mike Ford,  Electronic Information Services Adviser,
Learning Support Services, Learning & Information Services,
JG125, James Graham Building, Leeds Metropolitan University,
Headingley Campus, LEEDS,  LS6 3QS,  United Kingdom
Email: m.ford@xxxxxxxxxxxxxx
Tel: +44 113 283 2600 extn 4730      Fax:  +44 113 283 3211 

-- 
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