Jump to content

How can check variable value content integer or not by is_int() ?


Recommended Posts

$showCountSql = "select cad_count from counteraccountdtl WHERE cad_userid =".$_SESSION['UID']." LIMIT 1";

$showCountresult = mysql_query($showCountSql);

$showCountrow = mysql_fetch_array($showCountresult);

$newCount = $showCountrow[cad_count];

 

 

if(is_int($newCount))

echo "Value  is Integer";

else

echo "Value not Integer";

 

 

i m fetching value by Mysql "cad_count integer(5)" and  feild then i cheak this value is interger  or not it show the "Value not Integer". What is wrong in it ???

 

 

 

EDIT:

field type is int(5).

 

Then why do you need to validate it if the database is going to enforce it as an int?

 

Original Text:

 

What type of field is "cad_count" in the database? Is it a varchar or float type? If the value is always supposed to be an int then you should be using that type of field. However, if the field can be other than an int, then you could validate an int using ctype_digit() to verify that it is a positive integer. If you need to allow negative integers I can give you a different solution.

Database functions return strings, by definition, regardless of the field type.

 

From the php documentation for the function you are trying to use -

 

is_int — Find whether the type of a variable is integer

 

Description

bool is_int ( mixed $var )

Finds whether the type of the given variable is integer.

 

Note:

 

To test if a variable is a number or a numeric string (such as form input, which is always a string), you must use is_numeric().

 

 

 

yeah I don't really understand the logic for checking if it is an int coming right out of your db..

as to why the condition is failing.. I'm not sure really.. perhaps it is returning a numeric string.. hard to say..

 

EDIT:

 

PFM confirmed my thoughts... well noted

To test if a variable is a number or a numeric string (such as form input, which is always a string), you must use is_numeric().

 

The problem with is_numeric() is that it will return true for floats. Most times when I need something to be an int I typically need it to be an int with a value of 1 or greater. Such as when it need to be the primary id for a record. In that case I typically use something such as

$value = (int) $value; //Force value to be an integer
if($value<1)
{
    //Validation failed
}

 

But, it all depends on what are valid values for the variable you are using as to how you should validate it. But, as stated previously, if the field is an int type in the database - it will be an int. Unless of course you allow a null value in that field.

In verifying an idea to check for integers, I have made my head hurt. Can anyone explain this behavior?

 

<?php 

$a = 'aager12345a';
$b = $a;
$c = (int) $b;

var_dump($a); echo '<br>'; var_dump($c); echo '<br>';
if( $a == $c ) {
echo 'match';
} else {
echo 'no match';
}

?>

 

Outputs

 

string(11) "aager12345a" 
int(0) 
match

 

PHP 5.3.0 Apache 2.2.11 Windows 7 - Local

PHP 5.2.17 Apache ?? Redhat

Since $c was cast as an integer, I suspect php is type-juggling $a to an integer as well. That would cause its value in the comparison to be 0, allowing the comparison to evaluate to TRUE. A strict === comparison returns FALSE.

Since $c was cast as an integer, I suspect php is type-juggling $a to an integer as well. That would cause its value in the comparison to be 0, allowing the comparison to evaluate to TRUE. A strict === comparison returns FALSE.

 

Yes, http://php.net/manual/en/types.comparisons.php. Except I believe that $a is juggled to a float.  But still 0 == 0.0 and (int)0 == (float)0.

That's easy to compensate for, too.

 

if( ctype_digit(trim(str_replace('-', '', $num))) ) {

 

If you change that to ctype_digit(trim(ltrim($num,'-'))) it will only match '-1554' and not '15-54'

 

 

I don't know if this is more efficient or not, but it is easier to "see" the logic in my opinion:

if(is_numeric($val) && intval($val)==$val)

 

EDIT: Just found that the above returns true for something such as '12.0'. But, you could just wrap the above in a function and convert to an int before returning the value. This really shouldn't be that hard. If there is a strtotime() function there should be a function to validate if a string represents an integer or not.

 

EDIT #2: OK, this seems to work for any values.

if((string) (int) $val === (string) $val)

 

I, personally, would rather use a regular expression than try a bunch of type casting.

if (preg_match('/^[+-]?\d+\.?$/', $val)) // integer

Or a quick scan through for a period.

if (is_numeric($val) && strpos($val, ".") === false)

Or rounding.

if (round($val) == $val)

 

But still: why are you trying to validate an integer when you already know it's an integer?

I, personally, would rather use a regular expression than try a bunch of type casting.

if (preg_match('/^[+-]?\d+\.?$/', $val)) // integer

Or a quick scan through for a period.

if (is_numeric($val) && strpos($val, ".") === false)

Or rounding.

if (round($val) == $val)

 

But still: why are you trying to validate an integer when you already know it's an integer?

not sure how this thread got so hot.. there is no logic to begin with

I really don't understand why this thread has caused so much confusion. Reg expressions to check if a variable is an integer... really? Why?

 

Retrieving items from the database will give you an array of strings. So simply convert the strings stored in the integer field to integers either by type casting or using intval(). It's that simple.

I really don't understand why this thread has caused so much confusion. Reg expressions to check if a variable is an integer... really? Why?

Yeah, I know. But apparently an INT field type in the database isn't enough. And it's true that PHP doesn't have a function to check if a numeric string is strictly an integer.

Yeah, I know. But apparently an INT field type in the database isn't enough. And it's true that PHP doesn't have a function to check if a numeric string is strictly an integer.

?

 

http://www.php.net/manual/en/function.is-int.php

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.