Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

is_int problems 1

Status
Not open for further replies.

jgd12345

Technical User
Apr 22, 2003
124
GB
Hi, I know this may sound amatureish (I've tried searching for a solution but have not found anything). Well I went to php.net and found the function is_int to check if a variable is an integer. I have a simple form ie with one of the test boxes:

<input type="text" name="v2">

Then on the page where the information is processed (method = "post) I have:

if(!is_int($v2)) {
exit();
}

I thought it would check if it was an integer if not it would exit but it does not work.

I'd be greatful if anyone could help. Thanks

ps this may also help the above example has been simplified and the form is actually made up of text boxes with arrays. This is how the is_int is actually checked so it maybe something todo with this:

foreach($drawGameBoards as $v1) {
foreach($v1['numbersChosen'] as $v2) {
if(!is_int($v2) {
exit();
}
}
}
 
Insufficient information for a meaningful answer. What does "does not work" mean as you use it in this context?


If by "does not work", do you mean that your script says that no input is an integer, this is probably because, by definition, all inputs from a form are strings. Some of them are numeric strings, but they are strings nonetheless.

You could use is_numeric() to determine whether a value is a number or a numberic string. From there, making sure that the entire value is comprised of nothing but numeric digits will tell you if it's an integer.


Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Hi thanks for your response. By "does not work" I mean that it returns not an integer I understand why now because it is a numeric string not a numeric integer. I tried the is_numeric function and it worked fine but it also allows decimals. I strictly only want integer. I looked on php.net and found if(!eregi("[^0-9]", $v2)) but this does not work either.

I'd be greatful if you could help once more. Thanks
 
Given the following input form:
Code:
<html><body>
<form method="post" action="test_integer.php">
        <input type="text" name="a"><br>
        <input type="submit">
</form></body></html>

This script:
Code:
<?php

if (is_numeric($_POST['a']))
{
        if (preg_match ('/\D/', $_POST['a']))
        {
                print 'is a number, is not an integer';
        }
        else
        {
                print 'is an integer';
        }
}
else
{
        print 'is not a number';
}
?>

Will correctly tell me if an input value is not a number, a number but not an integer, or an integer.


Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Hi, Thanks once more sleipnir214 much appreciated. I'm also having trouble thinking of a way of recognising whether an array has the same value in it twice.

ie

$var[0] = 1;
$var[1] = 2;
$var[2] = 1;

This would return true since $var[0] and $var[2] have the same value 1.

The array is 3 values long (as above). I'm not sure if there's a built in function (I am yet to find it) or there's some other way.

I'd be greatful for your help. Thanks
 
if you want to programatically do it, create a new array sort it, the conpare the neighbouring values.

--------------------------
"two wrongs don't make a right, but three lefts do" - the unknown sage
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top