Best way to check for positive integer (PHP)?

前端 未结 20 2253
心在旅途
心在旅途 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:15

    the difference between your two code snippets is that is_numeric($i) also returns true if $i is a numeric string, but is_int($i) only returns true if $i is an integer and not if $i is an integer string. That is why you should use the first code snippet if you also want to return true if $i is an integer string (e.g. if $i == "19" and not $i == 19).

    See these references for more information:

    php is_numeric function

    php is_int function

提交回复
热议问题