The Web Blinders logo

Programming

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.

Need developers ?

if so, send a message.

thewebblinders@gmail.com

More Programming from our blog

SEARCH FOR ARTICLES