|
Posted by Pugi! on 02/12/07 20:32
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!
[Back to original message]
|