strange outcome when comparing int value against a give minimum and maximum value

hi,

I am using this code for checking wether a value (form input) is an
integer and wether it is smaller than a given maximum and greater then
a given minimum value:

function checkInteger(&$value, $checks) {
$err = '';
if (!is_numeric($value) || (floatval($value) != intval($value))) {
$err .= 'Input must be an integer. ';
} else {
$value = intval($value);
if ($checks['MinValue'] != '' && $value < $checks['MinValue']) {
$err .= 'Minimum value = ' . $checks['MinValue'] . '. ';
}
if ($checks['MaxValue'] != '' && $value > $checks['MaxValue']) {
$err .= 'Maximum value = ' . $checks['MaxValue'] . '. ';
}
}
}

and I would call the function like this :
$checks['MinValue'] = ($row['MinValue'] != '') ? $row['MinValue'] :
'';
$checks['MaxValue'] = ($row['MaxValue'] != '') ? $row['MaxValue'] :
'';
$err = checkInteger($value, $checks);

Most of the time the value for the minvalue and the maxvalue in the
array $checks comes from database(also with the type (int, decimal,
date, ...) and wether it is required or not). The $value is forminput.
This worked fine until I started using it with min and max values
assigned in the code. For example I would call the function with like
$maxValue = 59;
$minValue = 0;
$err = checkInteger($s, array('MaxValue' => $maxValue, 'MinValue' =>
$minValue));

The check for the maxvalue and wether it was an integer or not was ok.
But when the value was a negative integer and smaller then the
minValue it would pass the test !!!
I just couldn't figure it out. After quit some time I thought of
comparing the types of the variables using gettype after the line of
code where I set $value = intval($value);.

When it works (minValue and maxValue are obtained from database):
$value is integer (obviously)
$checks['MaxValue'] & $checks['MinValue'] are strings.

When it doesn't work:
$value is integer (obviously)
$checks['MaxValue'] & $checks['MinValue'] are integers.

So comparing an integer with a string, the program works fine. When a
compare an integer with an integer it doesn't work. Am I overlooking
something when comparing integers ? I've tested it with assigning
strings to min and max value in the code
$maxValue = '59';
$minValue = '0';
and it works fine. But why can't I use integers ?

I also works when I put intval in front of the $checks values in the
function checkInteger:

function checkInteger(&$value, $checks) {
$err = '';
if (!is_numeric($value) || (floatval($value) != intval($value))) {
$err .= 'Input must be an integer. ';
} else {
$value = intval($value);
if ($checks['MinValue'] != '' && $value <
intval($checks['MinValue'])) {
$err .= 'Minimum value = ' . $checks['MinValue'] . '. ';
}
if ($checks['MaxValue'] != '' && $value >
intval($checks['MaxValue'])) {
$err .= 'Maximum value = ' . $checks['MaxValue'] . '. ';
}
}
}

Code works now, but still don't understand why it work didn't in the
first place.
comparing integer with string : OK
comparing integer with integer : not OK
comparing integer with intval(string) : OK

Pugi!
puginews [ Mo, 12 Februar 2007 21:29 ] [ ID #1626713 ]

Re: strange outcome when comparing int value against a give minimum and maximum value

| function checkInteger(&$value, $checks) {
| $err = '';
| if (!is_numeric($value) || (floatval($value) != intval($value))) {
| $err .= 'Input must be an integer. ';
| } else {
| $value = intval($value);
| if ($checks['MinValue'] != '' && $value <
| intval($checks['MinValue'])) {
| $err .= 'Minimum value = ' . $checks['MinValue'] . '. ';
| }
| if ($checks['MaxValue'] != '' && $value >
| intval($checks['MaxValue'])) {
| $err .= 'Maximum value = ' . $checks['MaxValue'] . '. ';
| }
| }
| }


function checkInt($value, $min, $max)
{
if (!is_numeric($value)){ return false; }
if (floatval($value) != intval($value)){ return false; }
}

don't use magic variables like $checks. either make legit params like
$min/$max or make $checks an object. as it is, you don't even check to see
if $checks is an array. further, don't live in nests. if there are
conditions that must be met before your code should run, then begin by
dropping out of the function *at the first possible opportunity* ... that
keeps the code not only neater but definitively shows what the conditions
are.

as for why your code didn't work before...well, you have to show us what the
code *was* before. most likely, you didn't cast correctly.
Steve [ Mo, 12 Februar 2007 22:10 ] [ ID #1626714 ]
PHP » alt.php » strange outcome when comparing int value against a give minimum and maximum value

Vorheriges Thema: Jerrata Backbone PHP Operating System
Nächstes Thema: Newbie: How to generate multiple web pages with one PHP script