On 16 February 2011 22:04, Paul M Foster <paulf@xxxxxxxxxxxxxxxxx> wrote: > On Wed, Feb 16, 2011 at 09:21:20PM +0100, Peter Lind wrote: > >> On 16 February 2011 21:00, Dan Schaefer <dan@xxxxxxxxxxxxxxxxxxxx> wrote: >> > In my code, I set the optional parameter to NULL and check for triple >> equals >> > "===" or "!==" to see if the variable has been passed with a value. IMO, >> > this is the safest way. >> > >> > function MyFunction($x, $y, $z=NULL) { >> > if ($z !== NULL) { >> > // Do Something >> > } >> > } >> >> In case you're actually trying to test if a variable was passed or not >> that doesn't work (as it doesn't detect NULL passed in). Use >> func_get_args() as that gives you any and all arguments passed to the >> function, excluding defaults. > > This must be my week to make enemies. > > I beg to differ. If the user passes in NULL for $z or passes in nothing > for that variable, it does in fact show up as NULL. The following test > code works as indicated: Did anyone say it wouldn't? > > The responses in the first and third cases are NULL. You might be able > to argue that in the first case, where the user passed in NULL, it's > not the same as if the user passed in nothing. But I would assume that the > author considers NULL a signal value, such that if the user passed such > a value in, it would be equivalent to not passing a value for the third > parameter. The point I was trying to make was specifically that someone might try to distinguish whether a value was passed in for an optional parameter or not (like the case Adam outlined). Relying on the value of the optional parameter will not do, in that case (well it might, depending upon how stringent you really need to be). >> >> That said, if you're making use of optional parameters and need to >> check if anything was passed in, you're almost certainly doing things >> wrong. > > Again, assuming you're talking about examples like the one above, I have > to disagree. Presumably, passing in an optional parameter changes the > action inside the function. Thus, you would *need* to test for its > presence. Otherwise, you would be stuck with assuming it's there, > attempting to use it on that assumption, and dealing with whatever > errors it causes when-- surprise!-- it's not there. You should be wary of assumptions, they're not really working out for you. - a) you're not addressing Adam's use case - b) I wouldn't need to test for any presence if I wrote the function as I would personally prefer, i.e. not trying to test if any value was passed to the function or not. That's the whole point of optional values: even if nothing is passed in, there's a default value. That's why trying to check if something was passed for the optional parameter is counter-productive: the idea of the default value is that you shouldn't have to check. Regards Peter -- <hype> WWW: plphp.dk / plind.dk LinkedIn: plind BeWelcome/Couchsurfing: Fake51 Twitter: kafe15 </hype> -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php