Not a Number is a number

February 8th, 2007

I was having some trouble in PHP with a variable getting sent as NaN.
Try this out:

  1. <?
  2. echo " Is NaN a number: " . is_numeric(NAN);
  3. ?>

This is what I was doing in my PHP code:

  1. <?
  2. if ($homePhotoID== || !is_numeric($homePhotoID) ){
  3. $homePhotoID=‘NULL’;
  4. }
  5. ?>

I was using this variable to insert into a MySQL query. Debugging I figured out it was sending through: NaN
This is how my code ended up:

  1. <?
  2. if ($homePhotoID== || !is_numeric($homePhotoID) || is_nan($homePhotoID) ){
  3. $homePhotoID=‘NULL’;
  4. }
  5. ?>

Entry Filed under: PHP

Bookmark and Share   Del.icio.us   Technorati

2 Comments Add your own

  • 1. oz&hellip  |  2007-02-27 at 6.07 pm

    Hey Daniel, I just thought I’d ping you and let you know that I answered your question on my blog: http://polygeek.com/pxp2/tips-and-tricks/

    Nice stuff you have here. I look forward to reading more.

    Nice irony that NaN is a typeof Number. It reminds me of an old game that Douglas Adams did WAY back in the day. To finish the game you had to, and I quote, “have tea, not tea.”

    Yeah, that took a while to figure out as I’m sure your debugging for this issue here. Nice.

    By the way, Flash does the same thing:

    trace(typeof(NaN)); // output: number

  • 2. Danno&hellip  |  2007-02-28 at 9.49 am

    Thanks mate,

    It took me some google searches to find about that “no tea”.
    I haven’t played the game before. Here’s a link if anyone that is reading this would like to understand: http://everything2.com/index.p.....tnode_id=0

    cheers

Leave a Comment

Required

Required, hidden

Some HTML allowed:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Trackback this post  |  Subscribe to the comments via RSS Feed


Most Recent Posts