/    Sign up×
Community /Pin to ProfileBookmark

is_int() not working, returning false….

was just writing one of my first apps ever. am checking user input and using is_int() but it is returning false when i enter a number,…crimeny!

below is a distilled excerpt, ….pretty simple. can someone tell me why this returns false?! and btw, is there a convert to integer function, or maybe i can use settype()

<?php
$thisPage = $_SERVER[‘PHP_SELF’];
$theNum = $_
POST[‘thenum’];
if(isset($_POST[‘submit’])) {
if(is_int($theNum)) {
echo $theNum . ” is a number”;
} else { echo $theNum .” is not a number”;}
}

?>

<form action=”<?php $thisPage ?>” method=”post”>
<input type=”text” name=”thenum” />
<input type=”submit” value=”submit” name=”submit”>
</form>

big thanks for any help,

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@bionoidJan 18.2012 — When you extract the number value directly from $_POST, its data type is a [COLOR="Blue"]string[/COLOR].

You could use is_numeric to check:

[CODE]if ([COLOR="Red"]is_numeric[/COLOR]($theNum)) {
echo $theNum . " is a number";
} else {
echo $theNum . " is not a number";
}[/CODE]


or a regular expression

[CODE]if ([COLOR="red"]preg_match('/^d+$/', $theNum)[/COLOR]) {
echo $theNum . " is a number";
} else {
echo $theNum . " is not a number";
}[/CODE]


If you want to cast to an integer data type:

[CODE]$theNum = [COLOR="red"](int)[/COLOR]$_POST['thenum'];
echo [COLOR="Red"]is_int[/COLOR]($theNum) ? 'Number' : 'Not'; //Number[/CODE]
Copy linkTweet thisAlerts:
@toptomatoauthorJan 18.2012 — awesome, thank you
×

Success!

Help @toptomato spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.2,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...