24
$\begingroup$

Getting variable from form:

<form method = 'POST' action = ''>
        <input type = 'text' name = 'a' size = '1' >
        <input type = 'submit' value = 'Find it'>
</form>"

If I enter 1 and use gettype($POST_['a']) it returns me string, is it possible to enter int? because after this I want check if that variable is int.

UPDATE

Got answers that it returns always string and they offered me to use (int) or intval(), but then if it's really string like 'a' it returns 0, but it may be also integer value 0, how to overcome this problem?

UPDATE

After editing typo Brad Christie suggested best way, using is_numeric

$\endgroup$
1
  • 1
    $\begingroup$ There's a lot of questions like this on SO that show up in my interest list because they have the tag HTML. It's not really an HTML question it's a PHP question. Should I be de-tagging HTML? $\endgroup$ Commented Feb 19, 2011 at 19:23

5 Answers 5

32
$\begingroup$
// convert the $_POST['a'] to integer if it's valid, or default to 0
$int = (is_numeric($_POST['a']) ? (int)$_POST['a'] : 0);

You can use is_numeric to check, and php allows casting to integer type, too.

For actual comparisons, you can perform is_int.

Update

Version 5.2 has filter_input which may be a bit more robust for this data type (and others):

$int = filter_input(INPUT_POST, 'a', FILTER_VALIDATE_INT);

I chose FILTER_VALIDATE_INT, but there is also FILTER_SANITIZE_NUMBER_INT and a lot more--it just depends what you want to do.

$\endgroup$
Sign up to request clarification or add additional context in comments.

5 Comments

$_POST['a'] will never be an int so $int will always be zero with this.
@AndrewMarshall: Freudian slip on my part, I was thinking of is_numeric. Good catch, and I'll correct.
@sdleihssirhc: I initially used is_int, which is why the down vote. Thanks to @AndrewMarshall it was caught. -- @Templar: figured as much, and you're very welcome. ;-)
What if the posted object is an array of strings that should be integers, like a <select multiple="multiple" name="ids[]">? Do I need to iterate and convert each entry to int or is there a better method?
@David You can use array_map to do so: array_map('intval', $_POST['ids'])
19
$\begingroup$

Sending over the wire via HTTP, everything is a string. It's up to your server to decide that "1" should be 1.

$\endgroup$

Comments

5
$\begingroup$

No. HTTP only deals with text (or binaries).

You have to convert it.

$\endgroup$

1 Comment

int is not method. intval($a) or (int) $a
4
$\begingroup$

I'd use (int)$_POST['a'] to convert it to an integer.

$\endgroup$

Comments

1
$\begingroup$

if you prefer mantain a wide type compatibility and preserve input types other than int (double, float, ecc.) i suggest something like this:

$var = is_numeric($_POST['a'])?$_POST['a']*1:$_POST['a'];

You will get:

$_POST['a'] = "abc"; // string(3) "abc"
$_POST['a'] = "10"; // int(10)
$_POST['a'] = "10.12"; // float(10.12)
$\endgroup$

1 Comment

Yes, we might send floating numbers too ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.