Forum Moderators: coopster

Message Too Old, No Replies

Finding a whole number

         

dkin

4:18 am on May 9, 2005 (gmt 0)

10+ Year Member



I have an upgrade script where the user chooses a number to upgrade by, I do not want them to be able to upgrade by .3 or .8, only numbers divisibal by 1, How can I find if they are trying to use a decimal?

ironik

4:36 am on May 9, 2005 (gmt 0)

10+ Year Member



use is_float() to detect if the number is a decimal, or is_int() if the number is a whole number.

You'd probably do something like:

if (is_int($_POST['upgrade']))
{
// Proceed with the upgrade
} else {
// Spit out an error
}

dkin

4:37 am on May 9, 2005 (gmt 0)

10+ Year Member



lol your so useful. :D

dkin

4:52 am on May 9, 2005 (gmt 0)

10+ Year Member



I used it like this.

if (!is_int($str) ¦¦!is_int($sta) ¦¦!is_int($agi) ¦¦!is_int($dex))
{
echo 'Please Click the back button and use whole numbers when upgrading, Thank you.';
// Proceed with the upgrade
exit;
}

But no matter what it returns the error. what am I doing wrong?

ironik

4:59 am on May 9, 2005 (gmt 0)

10+ Year Member



Try using is_numeric() I'm guessing that is_int() might not be able to handle form data, which is returned as a string... wasn't thinking... ;)

dkin

5:05 am on May 9, 2005 (gmt 0)

10+ Year Member



Now no matter what it accepts the data, whether it is a decimal or not. I am using the exact same code except with is numeric.

Any ideas?

ironik

5:22 am on May 9, 2005 (gmt 0)

10+ Year Member



mmm... have to pull out the manual on that one. You can type cast the variable:

$str = (int)$_POST['str'];

... but I'm not sure of the result if it doesn't cast properly (might evaluate to null). Another way, since the form data is always a string, is to detect with a regex:

function isNumber($number)
{
return preg_match('/^[0-9]+$/', $number);
}

// then use it ...

if (isNumber($int) ¦¦ isNumber($str) ... )
{
// do stuff
} else {
// spit out error
}

Regex's consume more resources, but you'd get your desired result. The typecasting I'm not sure about the result if a non whole number is used.

Another method might be to make sure the post data is converted to a true integer by dividing it by one (an old trick, but I think it'll still work):

$str = $_POST['str']/1;
$int = $_POST['int']/1;
if (is_int($str) ¦¦ is_int($str) ... )
{

}

Try some of those and see how you get on.

dkin

5:11 pm on May 9, 2005 (gmt 0)

10+ Year Member



I have tried all of those and cant get any of them to work.

I must be quite inept, cant get working examples to work for me lol.

jatar_k

5:14 pm on May 9, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



well, you could also just use round [php.net] on any input

dkin

5:38 pm on May 9, 2005 (gmt 0)

10+ Year Member



But that would take more from the user then they are wanting to spend would it not?

if they entered 1.6, 2.5 and 3.8 it would remove 9 from their account. I just want it to return an error saying they must use whole numbers.

jatar_k

5:48 pm on May 9, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



well you could

use if is_numeric and match for a . in the submitted string

use floor to always round down

use round and give them a confirm page "did you really want to use X"

though the first is the best

if (!is_numeric($mynum) ¦¦!(strpos($mynum, ',')===false)) {
echo 'your number format is all messed up';
}

though I haven't had enough coffee yet that is close to right ;)

dkin

6:00 pm on May 9, 2005 (gmt 0)

10+ Year Member



if (!is_numeric($mynum) ¦¦!(strpos($mynum, ',')===false)) {
echo 'your number format is all messed up';
}

I do not understand that code.

What does

!(strpos($mynum, ',')===false)

do?

jatar_k

6:09 pm on May 9, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



funny, I put comma instead of . as while I was doing this I was thinking that, in french, they use a comma instead of .

should be

if (!is_numeric($mynum) ¦¦!(strpos($mynum, '.')===false)) {
echo 'your number format is all messed up';
}

just checks to see if there is a period in the string

timster

6:12 pm on May 9, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You could let PHP's string conversion function do the work, like so:

<?php
$str = 1;
$sta = "3";
$dex = "2";
$agi = "4";

if ( is_int_string($str) and is_int_string($sta) and is_int_string($dex) and is_int_string($agi)) {
echo "Integers or integer strings.";
} else {
echo "1 or more non-integers.";
}

function is_int_string($variant) {
return is_int(0 + $variant );
}
?>

This returns "Integers" but if the user enters "4.1" or even '4.0" it returns "non-integers."

dkin

6:20 pm on May 9, 2005 (gmt 0)

10+ Year Member



Jatar, How would I use

if (!is_numeric($mynum) ¦¦!(strpos($mynum, '.')===false)) {
echo 'your number format is all messed up';
}

With 4 different variables?

jatar_k

7:08 pm on May 9, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



test it on 1 first, see if it works, if it does, then make it a function and pass the value to test to it for each test

tmiller04

5:45 am on May 10, 2005 (gmt 0)

10+ Year Member



I haven't tested this one too carefully... but I believe it should work to verify a whole number.


function isWholeNum($num)
{
return((intval($num) - $num) === 0);
}

Let me know if you find a situation that it does not.

ironik

5:52 am on May 10, 2005 (gmt 0)

10+ Year Member



Something else I forgot to mention in my earlier post as an alternative to typecasting is to force a settype():

settype($_POST['str'], "integer"); // Forces any scalar variable to be an integer

There's so many ways to do the same thing, have you found a resolution to this yet?

dkin

6:59 am on May 10, 2005 (gmt 0)

10+ Year Member



I am not very advanced with php so all of these different solutions sort of confuse me. So no I have not found a solution to this problem yet.

dkin

5:04 pm on May 10, 2005 (gmt 0)

10+ Year Member



ok I just used (int)$var for all the numbers, it will work great. Thanks all :D

ironik

10:11 pm on May 10, 2005 (gmt 0)

10+ Year Member



Great! Just so long as you understand what it is you are writing you'll pick it up as you go along.

You've used typecasting, (which is not just a programming term), which tells the variable you are assigning to expect a certain variable type (in your case an integer). If you try putting a string in there I think it'll end up giving a value of null, but I could be wrong.