473,703 Members | 2,336 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($v alue) || (floatval($valu e) != 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($v alue, $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($v alue) || (floatval($valu e) != 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 3387
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($v alue) || (floatval($valu e) != 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($v alue) || (floatval($valu e) != 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($v alue)){ return false; }
if (floatval($valu e) != 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
1539
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 the same time. All the same things are running for each database and no one is using each database but me. From Query Analyzer I run a SQL against production three times in a row and it takes 1 minute 40 seconds on the last/best run. I then run...
20
2324
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
2897
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 as effective as possible. So I feel that using an int where only an uint is needed is a waste. e.g. something like (int i = 0; i < 100; i++)
6
23579
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 three and they validate a four items
4
2843
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 I'm just brute forcing my way through it), and during execution I build a list of 120 potential distances to be travelled, stored in an array of type double. I then have another function which traverses this array and locates the position of the...
7
28024
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 bit rgb data. Thanks!!
4
3594
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 accomplish it. Thanks, Scott
22
27054
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
1791
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 class ProgressController
0
8672
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9257
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9123
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8969
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6594
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5923
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4689
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3125
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2070
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.