Check if variable is a number and positive integer in PHP?

后端 未结 6 1467
甜味超标
甜味超标 2021-01-24 06:11

For example, say:


How do I check if variable

6条回答
  •  天命终不由人
    2021-01-24 07:00

    To check if a string input is a positive integer, i always use the function ctype_digit. This is much easier to understand and faster than a regular expression.

    if (isset($_GET['p']) && ctype_digit($_GET['p']))
    {
      // the get input contains a positive number and is safe
    }
    

提交回复
热议问题