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
Share on other sites

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.