type casting to float to "ensure reliable comparison" ?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • maxim vexler

    type casting to float to "ensure reliable comparison" ?

    in a book i am ready now : O'Reilly - Web Database Application with PHP
    and MySQL, 2nd ed. by David Lane, Hugh E. Williams

    on chapter 9 the author give an example for age validation :
    (simplified code, not a direct quote)

    $dob = mktime(0,0,0, 5, 3, 1983);

    if ((float)$dob > (float)strtotim e("-18years"))
    {
    print "....";
    return false;
    }

    my question is why the type casting to float ?
    after all the return type of both mktime & strtotime is int, why the
    need to cast to float?
    the authors emphasize this in the explanation section by "We use the
    mktime() function to convert the date of birth to a large numeric Unix
    timestamp value, and the strtotime() function to discover the timestamp
    of exactly 18 ago. *Both are cast to a large floating number to ensure
    reliable comparison...*"

    thank you for comment's ohh & yes - hope I'm not breaking any copyright
    lows :).
  • Chung Leong

    #2
    Re: type casting to float to "ensure reliable comparison&quot ; ?


    "maxim vexler <hq4ever (at) 012 (dot) net (dot) il> >" <"maxim vexler
    <hq4ever (at) 012 (dot) net (dot) il> wrote in message
    news:41150ecc@n ews.012.net.il. ..[color=blue]
    > in a book i am ready now : O'Reilly - Web Database Application with PHP
    > and MySQL, 2nd ed. by David Lane, Hugh E. Williams
    >
    > on chapter 9 the author give an example for age validation :
    > (simplified code, not a direct quote)
    >
    > $dob = mktime(0,0,0, 5, 3, 1983);
    >
    > if ((float)$dob > (float)strtotim e("-18years"))
    > {
    > print "....";
    > return false;
    > }
    >
    > my question is why the type casting to float ?
    > after all the return type of both mktime & strtotime is int, why the
    > need to cast to float?
    > the authors emphasize this in the explanation section by "We use the
    > mktime() function to convert the date of birth to a large numeric Unix
    > timestamp value, and the strtotime() function to discover the timestamp
    > of exactly 18 ago. *Both are cast to a large floating number to ensure
    > reliable comparison...*"
    >
    > thank you for comment's ohh & yes - hope I'm not breaking any copyright
    > lows :).[/color]

    I don't see any need for casting. Anyone?

    The author's use of a Unix timestamp to store a birthday makes me suspect
    his experience with PHP. It is rather well known that mktime() on PHP-Win32
    can't handle dates before 1970. The code would still work, but for a book,
    it is a poor example.


    Comment

    Working...