Forum Moderators: coopster

Message Too Old, No Replies

Is a whole number

quick one

         

ukgimp

3:59 pm on Oct 16, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I have a script that takes a variable from the query string and is used to LIMIT but I wa to make sure that no one can mess with it. It should be a multiple of $n

which function does the following. I am confused with the options

if($n/$s = a whole integer)
{
do something
}
else
{
do something else
}

Cheers

jatar_k

4:17 pm on Oct 16, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



is_int
!is_float
fmod

a couple of ideas

coopster

4:17 pm on Oct 16, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Use modulus:

if($n % $s)

% is modulo or modulus

<edit>added a link to the manual</edit>

Wait a minute...modulus may only work on integers! At least I'm pretty sure that's how Perl [perldoc.com] handles it, and I'm willing to bet PHP does the same...

[edited by: jatar_k at 9:19 pm (utc) on Oct. 16, 2003]
[edit reason] edit as per coopster [/edit]

coopster

8:10 pm on Oct 16, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Well, I finally tested this and it seems as though the PHP modulus operator (%) works on integer operands. If PHP operates like Perl, then the modulus operator (%) converts its operands to integers before finding the remainder according to integer division. And from my testing, it sure seems to be the case.

<?php
$dividend = 7;
$divisor = 2.4;
print $dividend%$divisor; // prints 1
print fmod($dividend, $divisor); // prints 2.2
?>

This is really something to remember, especially if you contrast it to some of the other scripting languages, one of which comes to mind is MS JScript h**p://msdn.microsoft.com/library/default.asp?url=/library/en-us/script56/html/js56jsoprmod.asp

Anyway, I would use fmod [us3.php.net] as jatar_k suggested. If it is user-supplied data via a form POST, is_int [us3.php.net] will return false because even though it may be a number that the user keyed, the variable brought in is going to be of type string. Even if it is a string numeric, such as '123', is_int will return false. Don't believe me? Try it:

<html><head><title>is_numeric</title></head><body>
<h1>is_int</h1>
<p>
<code>
<?php
if ($_POST['Submit']) {
$number = $_REQUEST['number'];
if (is_int($number)) {
print '$number is ' . $number . '<br />';
} else {
print '$number is ' . gettype($number) . '<br />';
}
if (is_numeric($number) and fmod($number, 2)) {
print 'fmod($number, 2) is ' . fmod($number, 2) . '<br />';
} else {
print 'fmod($number, 2) is ' . gettype($number) . '<br />';
}
}
?>
<form action="<?php print $_SERVER['PHP_SELF'];?>" method="post">
Number: <input type="text" name="number" />
<input type="submit" name="Submit" value="Submit" />
</form>
</code></p></body></html>

By the way, it is important to check that the value the user is supplying you actually is numeric [us3.php.net] before using it in fmod, otherwise, if a text string like 'abc' is POSTed, your code is going to choke and you are going to get a warning message similar to:

Warning: fmod() expects parameter 1 to be double, string given in /your/path/your_code.php on line 21.

Regards,
coopster

MonkeeSage

10:53 pm on Oct 16, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Just to add some more info to the fray, intval() will convert floats to ints by truncation (instead of by rounding as with round()).

Jordan