472,103 Members | 1,554 Online

# 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!

Feb 12 '07 #1
2 3212 Pugi! said:
>
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'] . '. ';
}
}
}
That doesn't seem to be javascript unless they've added a "." operator
while I wasn't looking. Typically if you are having trouble comparing
numbers in javascript, it's because you're really comparing strings
that represent numbers, and they are being compared lexically. That
might also apply to whatever it is that you're doing.
--

Feb 12 '07 #2
| 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.
Feb 12 '07 #3

### This discussion thread is closed

Replies have been disabled for this discussion.