Jump to content

integer or not


birbal
 Share

Recommended Posts

test.php

<?phpif(is_int($_GET['bid'])){echo "int";}else{echo " not int";}?>

Passed valuelocalhost/test.php?bid=1outputsurprisingly!! it is showing not intwhy it is showing this?

Link to comment
Share on other sites

the php manual explains ithttp://php.net/manual/en/function.is-int.php

To test if a variable is a number or a numeric string (such as form input, which is always a string), you must use is_numeric().
Link to comment
Share on other sites

yes.i see that. but i am not getting when i am sending a value via get why the test.php assume it as a string? it seems to me php will auto detect the type of variable.so should not it be a integer?another problem is....
quoted from php.net....'42' is numeric'1337' is numeric'1e4' is numeric'not numeric' is NOT numeric'Array' is NOT numeric'9.1' is numeric
here 1e4 (dont know what it is... guessing some exponential expression) and 9.1 is valid as numeric. but i want only integer not any float value or any 1e4 like expression. actually i want to validate it as a integer only Edited by birbal
Link to comment
Share on other sites

yes.i see that. but i am not getting when i am sending a value via get why the test.php assume it as a string? it seems to me php will auto detect the type of variable.so should not it be a integer?
To test if a variable is a number or a numeric string (such as form input, which is always a string), you must use is_numeric().
In other words, anything sent via POST or GET is considered a string and will be treated as such unless the situation demands type casting. And by that I mean:$test = $_GET['bid'] + 5;In this situation $_GET['bid'] will be casted into a number, but only for the duration of that calculation. Outside of that statement, $_GET['bid'] is still a string. Edited by ShadowMage
Link to comment
Share on other sites

another problem is....here 1e4 (dont know what it is... guessing some exponential expression) and 9.1 is valid as numeric. but i want only integer not any float value or any 1e4 like expression. actually i want to validate it as a integer only
There's a good test for that listed on the page that thescientist linked:
// First check if it's a numeric value as either a string or numberif(is_numeric($int) === TRUE){   if((int)$int == $int){ // It's a number, but it has to be an integer	  //Do something if it's an integer   }else{ // It's a number, but not an integer	  //Do something if it's NOT an integer   }}

And yes, 1e4 is an exponential notation. It means 1 * 10^4 or 10000

Edited by ShadowMage
Link to comment
Share on other sites

thanks to shadowmage wirehooper scinetist..now it is becomin clear. but still have problem. did you see that?

another problem is....QUOTEquoted from php.net....'42' is numeric'1337' is numeric'1e4' is numeric'not numeric' is NOT numeric'Array' is NOT numeric'9.1' is numerichere 1e4 (dont know what it is... guessing some exponential expression) and 9.1 is valid as numeric. but i want only integer not any float value or any 1e4 like expression. actually i want to validate it as a integer only
Link to comment
Share on other sites

There's a good test for that listed on the page that thescientist linked:
// First check if it's a numeric value as either a string or numberif(is_numeric($int) === TRUE){   if((int)$int == $int){ // It's a number, but it has to be an integer	  //Do something if it's an integer   }else{ // It's a number, but not an integer	  //Do something if it's NOT an integer   }}

And yes, 1e4 is an exponential notation. It means 1 * 10^4 or 10000

if(is_numeric($int) === TRUE)what does the === means?? Edited by birbal
Link to comment
Share on other sites

if(is_numeric($int) === TRUE)what does the === means??
It's the type comparison operator. It compares value and type whereas '==' only compares the values.So:('23' == 23) will return true but('23' === 23) will return falseSee the manual page I linked for more details.
Link to comment
Share on other sites

is exactly equal to, and is 'stricter' than ==. this won't typecast the value on the right like == would. For situations when you want to ensure the type and value of both parts of the equation.

Link to comment
Share on other sites

ohhhh..ok thanks again got to know a new thing. i will check that now. by the way i just see ctype_digit() in the php.net manual. it seems to me that will too work in this case.thanks again to shadow and scientist :)

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...