birbal Posted October 21, 2010 Share Posted October 21, 2010 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 More sharing options...
thescientist Posted October 21, 2010 Share Posted October 21, 2010 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 More sharing options...
birbal Posted October 21, 2010 Author Share Posted October 21, 2010 the php manual explains ithttp://php.net/manual/en/function.is-int.php 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 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 More sharing options...
ShadowMage Posted October 21, 2010 Share Posted October 21, 2010 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. Link to comment Share on other sites More sharing options...
wirehopper Posted October 21, 2010 Share Posted October 21, 2010 Note: 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().From the aforementioned link. Link to comment Share on other sites More sharing options...
ShadowMage Posted October 21, 2010 Share Posted October 21, 2010 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 onlyThere'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 Link to comment Share on other sites More sharing options...
birbal Posted October 21, 2010 Author Share Posted October 21, 2010 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 More sharing options...
ShadowMage Posted October 21, 2010 Share Posted October 21, 2010 thanks to shadowmage wirehooper scinetist..now it is becomin clear. but still have problem. did you see that?Yep, my previous post addresses that. Link to comment Share on other sites More sharing options...
birbal Posted October 21, 2010 Author Share Posted October 21, 2010 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?? Link to comment Share on other sites More sharing options...
ShadowMage Posted October 21, 2010 Share Posted October 21, 2010 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 More sharing options...
thescientist Posted October 21, 2010 Share Posted October 21, 2010 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 More sharing options...
birbal Posted October 21, 2010 Author Share Posted October 21, 2010 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 More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.