Jump to content

Using is_int


SlinkyABC

Recommended Posts

Hi,

 

I did a search for this function and found a similar post but wasn't able to understand it.

 

Presently I am just trying to do a simple test with the is_int function. I have it set up as follows:

 

if (is_int ($variable)) {
          echo 'Integer';
          } else {
          echo 'Not integer';

 

$variable is converted to a variable from some text on a web form.

 

I can see the problem here is that no matter what is entered on the web form, $variable is always a string. The code I referenced above works fine if I instead use the is_string function. I guess I'm just trying to figure out the following:

 

1. Why is it always a string, even when entering a number such as 7?

2. How can I overcome this so that I can check to make sure the input is of the correct type (integer)?

 

I know I'm probably not doing a good job of explaining what I mean but I think what I'm aiming to do should be clear; let me know if it isn't.

 

 

NOTE--I think I get this to a degree--anything passed in a form will be a string, is that correct? So I should be able to use is_numeric instead. However, the problem with that is that it would also return True for non-integers such as floats. I only want the user to be able to enter integers, 8.5 should return False in this case.

 

Thanks!

 

 

Link to comment
https://forums.phpfreaks.com/topic/249960-using-is_int/
Share on other sites

Try using this function

function is_really_int(&$val) {
        $num = (int)$val;    
        if ($val==$num) {
            $val=$num;
            return true;
        }
        return false;
    }

 

Loads more information here http://php.net/manual/en/function.is-int.php

Link to comment
https://forums.phpfreaks.com/topic/249960-using-is_int/#findComment-1282920
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.