PHP - Validating an input as a positive integer.
isPositiveInteger returns false if an input is not a positive integer.
<?php
function isPositiveInteger($val)
{
$filter_options = array(
'options' => array('min_range' => 0),
);
return filter_var($val, FILTER_VALIDATE_INT, $filter_options);
}
var_dump(isPositiveInteger(-123));
var_dump(isPositiveInteger('-123'));
var_dump(isPositiveInteger(123));
var_dump(isPositiveInteger('123'));
var_dump(isPositiveInteger("sfgfg"));
var_dump(isPositiveInteger(0));
This function will come handy for things like pagination where you want page number to be positive.