Let’s say you have a method that expects a numerical value as an argument. What’s the proper way to set a default value in the event the argument doesn’t exist?
public function saveme($myvar = false) {}
public function saveme($myvar = '') {}
public function saveme($myvar = NULL) {}
public function saveme($myvar = 0) {}
All of those are valid, and neither one is “more correct” for all cases. It depends entirely on what the valid range of values are for the function and what the function is supposed to do. Typically, the default type will match the expected input type, unless you want a place-holder for “no value” in which case
nullis typically used.If your function can perform something useful with default of
0, use0. If your function needs to tell the difference between having been explicitly given a single argument of0and having been given no arguments, or if0is not a sane default, usenull.