Is there a nicer way to do this?
if( $_POST['id'] != (integer)$_POST['id'] )
echo 'not a integer';
I've tried
if( !is_int($_POST['id']) )
But is_int()
doesn't work for some reason.
My form looks like this
<form method="post">
<input type="text" name="id">
</form>
I've researched is_int()
, and it seems that if
is_int('23'); // would return false (not what I want)
is_int(23); // would return true
I've also tried is_numeric()
is_numeric('23'); // return true
is_numeric(23); // return true
is_numeric('23.3'); // also returns true (not what I want)
it seems that the only way to do this is: [this is a bad way, do not do it, see note below]
if( '23' == (integer)'23' ) // return true
if( 23 == (integer)23 ) // return true
if( 23.3 == (integer)23.3 ) // return false
if( '23.3' == (integer)'23.3') // return false
But is there a function to do the above ?
Just to clarify, I want the following results
23 // return true
'23' // return true
22.3 // return false
'23.3' // return false
Note: I just figured out my previous solution that I presented will return true for all strings. (thanks redreggae)
$var = 'hello';
if( $var != (integer)$var )
echo 'not a integer';
// will return true! So this doesn't work either.
This is not a duplicate of Checking if a variable is an integer in PHP, because my requirements/definitions of integer is different than theres.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…