[thelist] Bad tip on my part

Jon Haworth evolt at laughing-buddha.net
Fri Mar 1 13:16:00 CST 2002


> function daysInFebruary($s) {
> // int daysInFebruary(mixed var)
> // Returns number of days in February for
> a valid four digit year, FALSE otherwise.
<snip>

Nice though that is, here's something to consider:

function daysInMonth ($month = 0, $year = 0) {
  if (0 == $month) $month = date ("n");
  if (0 == $year)  $year  = date ("Y");
  if (true == checkdate ($month, 1, $year)) {
    return date ("t", mktime (3, 0, 0, $month, 1, $year));
  } else {
    return false;
  }
}



Often repeated, but always worth it:

<tip type="variable comparisons">

If you compare variables like this:
if ($foo == false)

you will inevitably spend hours of your life looking for places where
you've accidentally messed up your program by assigning the value to the
variable by mistake.
if ($foo = false) // this will return true

On the other hand, if you compare variables like this:
if (false == $foo)

then your compiler/interpreter should throw an error if you make the same
mistake.
if (false = $foo) // you can't assign a variable to a constant :-)

</tip>


Cheers
Jon





More information about the thelist mailing list