How to get int instead string from form?

后端 未结 5 1348
北海茫月
北海茫月 2020-11-28 14:15

Getting variable from form:

5条回答
  •  温柔的废话
    2020-11-28 14:52

    if you prefer mantain a wide type compatibility and preserve input types other than int (double, float, ecc.) i suggest something like this:

    $var = is_numeric($_POST['a'])?$_POST['a']*1:$_POST['a'];
    

    You will get:

    $_POST['a'] = "abc"; // string(3) "abc"
    $_POST['a'] = "10"; // int(10)
    $_POST['a'] = "10.12"; // float(10.12)
    

提交回复
热议问题