Numeric String

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
hmsg
Forum Commoner
Posts: 42
Joined: Sun May 14, 2006 9:48 am

Numeric String

Post by hmsg »

Hello!

I have field in a form that is receveing a number, i need to test if is a integer, how do i do that?
If i could use de is_int() function my problem woul be resolved, but i can't because when the value comes by a form is a numeric string, and i only could use the function is_numeric(), but that function doesn't interest me because my number must be a integer and the is_numeric() function acepts decimal numbers.

Is there anyway to test my variable?


With the best regards

Hugo Gomes
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post by CoderGoblin »

Cannot understand why is_int shouldn't work but must admit to not using it...

One way of doing it is to check the value against the floor of the value. If they are the same it's an integer.

Code: Select all

if ($value==floor($value)) {
  //Its an integer
}
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

CoderGoblin wrote:Cannot understand why is_int shouldn't work but must admit to not using it...
because is_int checks the (internal) variable type and http paraemter always ship as strings.

Code: Select all

<?php
function foo(&$bar) {
	echo gettype($bar), "<br />\n";
	echo 'value: ', $bar, "<br />\n";
	echo 'is_int:', is_int($bar) ? 'yes':'no', "<br />\n";
	echo 'is_string:', is_string($bar) ? 'yes':'no', "<br />\n";
	echo "<hr />\n";
}

$i = 1;
$s = '1';

foo($i);
foo($b);
?>
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

Use typecasting..

Code: Select all

$value = (int) $value;
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
Cameri
Forum Commoner
Posts: 87
Joined: Tue Apr 12, 2005 4:12 pm
Location: Santo Domingo, Dominican Republic

Post by Cameri »

if you just require to check if it is a number, you should do like scottayy said, typecasting, that is.
Now, if you need to check if the number is within a valid range, something logical, lets say there's no reason why an user should input 9999999, if the number should be between 1000 and 3000, then after you typecast, you just check that the number is in that range.

Code: Select all

$myInt = (int)$_POST['myInt'];
if ($myInt >= 1000 && $myInt<=3000) {
 //user posted a valid integer
}
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Cameri wrote:Now, if you need to check if the number is within a valid range, something logical, lets say there's no reason why an user should input 9999999, if the number should be between 1000 and 3000, then after you typecast, you just check that the number is in that range.
I prefer to simply force it into the range if it's that case.

Code: Select all

$num = min(max((int)$num, $min), $max);
Post Reply