Best way to check for positive integer (PHP)?

前端 未结 20 2285
心在旅途
心在旅途 2020-12-02 18:34

I need to check for a form input value to be a positive integer (not just an integer), and I noticed another snippet using the code below:

$i = $user_input_v         


        
20条回答
  •  悲&欢浪女
    2020-12-02 19:09

    All these answers overlook the fact that the requestor may checking form input.
    The is_int() will fail because the form input is a string.
    is_numeric() will be true also for float numbers.
    That is why the $i == round($i) comes in as it checks for the input being a whole number.

提交回复
热议问题