php validate integer

前端 未结 7 1324
南旧
南旧 2020-11-28 10:43

I`m wonder why this not working

    echo gettype($_GET[\'id\']); //returns string
  if(is_int($_GET[\'id\']))
  {
   echo \'Integer\';
  }

7条回答
  •  独厮守ぢ
    2020-11-28 11:13

    The manual says:

    To test if a variable is a number or a numeric string (such as form input, which is always a string), you must use is_numeric().

    Alternative you can use the regex based test as:

    if(preg_match('/^\d+$/',$_GET['id'])) {
      // valid input.
    } else {
      // invalid input.
    }
    

提交回复
热议问题