php validate integer

前端 未结 7 1323
南旧
南旧 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:28

    Try:

    if(isNumeric($_GET['id'])) {
        $cast_int = (int)$_GET['id'];
    }
    
    if(isset($cast_int)) {
        echo gettype($cast_int)."
    \n"; if(is_int($cast_int)) { echo 'Integer'."
    \n"; } } else { echo gettype($_GET['id'])." was passed
    \n"; } function isNumeric($numeric) { return preg_match("/^[0-9]+$/", $numeric); }

提交回复
热议问题