473,503 Members | 12,175 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3362
Lee
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
1533
by: MGB | last post by:
I have a strange performance question hopefully someone can clarify for me. I take a production database and make a copy of it, called test, on the same instance on the same server both running at...
20
2288
by: Markus Sandheide | last post by:
Hello! Execute these lines: int x = 1; x = x > 2345678901; You will get: x == 1 with Borland C++ Builder
36
2841
by: Song Yun Zhao | last post by:
Hi, Just wondering what are the dis/advantages of using uint vs int. When would be the best time to use it? Personally I don't use uint that much, but I like to optimize my code and make it...
6
23390
by: WindAndWaves | last post by:
Hi Folks I have inhereted a script that I understand reasonably well, I just do not understand !/^\d+$/.test(el.value) what the hell does that mean? Below is the script (there are really...
4
2836
by: Matt Billock | last post by:
Hello everyone, I am having some issues with what should be an insanely simple problem. Basically, I'm working on an implementation of the TSP algorithm (I have a static set of 5 locations, so...
7
27991
by: anuragkhanna8 | last post by:
Hi, I am trying to convert a 16 bit rgb value to 32 bit, however the new color generated is different from the 16 bit rgb data. Please let me know the formula to convert an 16 bit rgb data to 32...
4
3575
by: Scott | last post by:
In order to give a meaning average value and minimum and maximum values, I would like to have a formula for a group of data after taking out those extremes. Can someone share your way to...
22
26999
by: subramanian100in | last post by:
Consider the following program #include <limits.h> #include <stddef.h> int main(void) { size_t size; size_t bytes = sizeof(size_t);
0
1777
by: flyingchen | last post by:
using System; using System.Windows.Forms; using System.Threading; using System.ComponentModel; namespace ProgressControl.Core { public delegate object Execute(params object args); public...
0
7193
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7067
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7264
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7316
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6975
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7449
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5562
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4666
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3148
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.