checking data type - integer from form

Discussion in 'PHP' started by mdrobiul, Oct 24, 2009.

  1. #1
    a value is come from my form and I want to check if it is integer.

    I'm trying to check with gettype() but it is returning "string".

    the source of this value is html form. i'm trying to check like this

    if (gettype($value)== 'integer'){do the task}

    if i echo gettype($value); it prints "string"

    and if i echo $value; it prints "45427"

    it's not being checked as integer although $value prints integer. Where is the problem ? any alternative way with php except javascript?
     
    mdrobiul, Oct 24, 2009 IP
  2. mastermunj

    mastermunj Well-Known Member

    Messages:
    687
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    110
    #2
    mastermunj, Oct 24, 2009 IP
  3. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #3
    JAY6390, Oct 24, 2009 IP
  4. mastermunj

    mastermunj Well-Known Member

    Messages:
    687
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    110
    #4
    somehow php.net is not opening at my side so gave link for other site, however it solves the purpose :)
     
    mastermunj, Oct 24, 2009 IP
  5. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #5
    jestep, Oct 24, 2009 IP
  6. szalinski

    szalinski Peon

    Messages:
    341
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #6
    that's not an integer, it's a string, see the 'number' has quotes around it. if you var_dump($value) you'll see string(5) "45427".

    You could try using $value = intval($_POST['value']) or however you catch it to enforce integer type, so when you var_dump after that you should see int(5) 45427.
     
    szalinski, Oct 24, 2009 IP
  7. xenon2010

    xenon2010 Peon

    Messages:
    237
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #7
    there is a php builtin function called isnumber, it will return true if the string only numbers and false if not..
     
    xenon2010, Oct 24, 2009 IP
  8. mdrobiul

    mdrobiul Peon

    Messages:
    186
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    thanks guys.
     
    mdrobiul, Oct 25, 2009 IP