Hey guys hopefully this is just a quick question.

I've a form that is sending data to a PHP script. The form has the following textbox:

<input id="quantity1" name="quantity1" type="text" value="" />

Whenever it is posted I do the following:

function isInteger($val) {
  if (is_int($val)) {
    return true;
  } else {
    return false;
  }
}

$test = $_POST["quantity1"];

echo gettype($test); // Which is returning as a string

  if (  isInteger($test)  ) {
    echo "<p>It's an integer!</p>";
  } else {
    echo "<p>It's not an integer!</p>";
  }

Even if I type a number such as '1' or '123' into the textbox, it's always being picked up by the script as string type. Is there anyway to convert this into an integer, or even hardcoding the $test variable to be strictly an integer?

Thanks in advance,

Anthony

try is_numeric() instead... is_int is a really specific function that only works when calling it like

is_int(1); //true
is_int('1'); //false
Comments
Sound advice.

Thats great man, the is_numeric() function works great, although if I run a gettype() on the variable, it still appears as a string.

So really the is_numeric() function is looking for a string that contains numbers right?

Almost... the is_numeric checks the variable you passed to it is a number - of any kind. If you pass "nvg8vn4g4g444" to it, it should return false...

Yep your right, thats returning false for me. The function is working perfectly now, thanks very much for all your help. I've added to your rep and this is marked as solved!

Anthony

This question has already been answered. Start a new discussion instead.