Warptweet Posted August 24, 2007 Share Posted August 24, 2007 I have a form that asks my users for the width and height of their submission in pixels, but I want to prevent my users from entering a letter or a word to try and trick the system. Is there a way of checking if my variable $variable is an integer or a string (or even a combination)? I'm pretty sure this is very easy, but for some reason I can't put my finger on it. Quote Link to comment https://forums.phpfreaks.com/topic/66430-solved-checking-if-a-variable-is-an-integer/ Share on other sites More sharing options...
dbo Posted August 24, 2007 Share Posted August 24, 2007 $flag = is_int($var); or $matches = preg_match("^\d+$", $var); if( $matches != 0 ) $flag = intval($var); Quote Link to comment https://forums.phpfreaks.com/topic/66430-solved-checking-if-a-variable-is-an-integer/#findComment-332538 Share on other sites More sharing options...
trq Posted August 24, 2007 Share Posted August 24, 2007 is_int won't work because all data retrieved via POST or GET is actually a string. Use is_numeric(). Quote Link to comment https://forums.phpfreaks.com/topic/66430-solved-checking-if-a-variable-is-an-integer/#findComment-332539 Share on other sites More sharing options...
teng84 Posted August 24, 2007 Share Posted August 24, 2007 is_int won't work because all data retrieved via POST or GET is actually a string. Use is_numeric(). then after using thorpes suggestion use int sample <?php echo (int) ( (0.1+0.7) * 10 ); // echoes 7! ?> note numeric accept" + " and etc....see thorpes link Quote Link to comment https://forums.phpfreaks.com/topic/66430-solved-checking-if-a-variable-is-an-integer/#findComment-332544 Share on other sites More sharing options...
dbo Posted August 24, 2007 Share Posted August 24, 2007 Doh! I always use regex anyways. But no matter what you do I suggest after doing your check you do a cast to the datatype that it should be. Apparently regex has some oddities where $ doesn't necessarily mean the end of the line. Quote Link to comment https://forums.phpfreaks.com/topic/66430-solved-checking-if-a-variable-is-an-integer/#findComment-332552 Share on other sites More sharing options...
Warptweet Posted August 24, 2007 Author Share Posted August 24, 2007 $flag = is_int($var); or $matches = preg_match("^\d+$", $var); if( $matches != 0 ) $flag = intval($var); Okay, apparently is_int won't work. So for your second code, how do I make it execute code inside { and } if it IS an int? Or, does that make the variable an integer, so I don't need to do an if statement? Quote Link to comment https://forums.phpfreaks.com/topic/66430-solved-checking-if-a-variable-is-an-integer/#findComment-332555 Share on other sites More sharing options...
dbo Posted August 24, 2007 Share Posted August 24, 2007 Oh wow, I really gave you bad code, whoops! As others pointed out apparently is_int wont work. Try this. function IsInteger($val) { $matches = preg_match("^\d+$", $val); if( $matches != 0 ) { return true; } return false; } usage: $val = "5"; $is_int = IsInteger($val); if( $is_int ) { $val = intval($val); } else { echo "ERROR! Not an int!"; } Quote Link to comment https://forums.phpfreaks.com/topic/66430-solved-checking-if-a-variable-is-an-integer/#findComment-332564 Share on other sites More sharing options...
dbo Posted August 24, 2007 Share Posted August 24, 2007 Actually instead of \d+ you should probably use [0-9]+ Quote Link to comment https://forums.phpfreaks.com/topic/66430-solved-checking-if-a-variable-is-an-integer/#findComment-332566 Share on other sites More sharing options...
dbo Posted August 24, 2007 Share Posted August 24, 2007 Hrmm. A thought occured to me so I'm going to throw it out here and see what some of the other people think. I think it may not be a "best practice" but here it is anyways. Try this. function IsInteger(&$val) { $matches = preg_match("^\d+$", $val); if( $matches != 0 ) { $val = intval($val); return true; } return false; } usage: $val = "5"; $is_int = IsInteger($val); if( $is_int ) { //Process normally... we don't need to cast since we passed by reference } else { echo "ERROR! Not an int!"; } Quote Link to comment https://forums.phpfreaks.com/topic/66430-solved-checking-if-a-variable-is-an-integer/#findComment-332574 Share on other sites More sharing options...
MadTechie Posted August 24, 2007 Share Posted August 24, 2007 why not keep it simple IE $X = "7.4"; $isint = (bool)($X == ((int)$X)); var_dump($isint); Quote Link to comment https://forums.phpfreaks.com/topic/66430-solved-checking-if-a-variable-is-an-integer/#findComment-332575 Share on other sites More sharing options...
Warptweet Posted August 24, 2007 Author Share Posted August 24, 2007 None of your code works. How about keep this even simpler? How can I check if a number is divisable by 1? And if it isnt, make $error .= "You entered an incorrect width."; After all, a letter cannot be divisable by 1, therefore it should be a logical method. Quote Link to comment https://forums.phpfreaks.com/topic/66430-solved-checking-if-a-variable-is-an-integer/#findComment-332581 Share on other sites More sharing options...
MadTechie Posted August 24, 2007 Share Posted August 24, 2007 try $X = "test"; $isint = (bool)(($X == ((int)$X)) && is_numeric($X)); var_dump($isint); echo "$X"; Quote Link to comment https://forums.phpfreaks.com/topic/66430-solved-checking-if-a-variable-is-an-integer/#findComment-332584 Share on other sites More sharing options...
dbo Posted August 24, 2007 Share Posted August 24, 2007 What do you mean it doesn't work? The first code I spewed was the suck but those last 2 should work fine. And yeah the madmans code should work fine, but its not perty! Whatever you do, turn it into a function and reuse the code. Don't write that stuff by hand each time. On another note.... any thoughts from anyone about passing that by reference and doing the cast? Quote Link to comment https://forums.phpfreaks.com/topic/66430-solved-checking-if-a-variable-is-an-integer/#findComment-332590 Share on other sites More sharing options...
Warptweet Posted August 24, 2007 Author Share Posted August 24, 2007 Sorry for being so inexperienced, but what do I do with madtechies code? Which part of it can I put $error .= "Invalid Width."; Which part of it can I put echo "Your width is valid!"; Quote Link to comment https://forums.phpfreaks.com/topic/66430-solved-checking-if-a-variable-is-an-integer/#findComment-332592 Share on other sites More sharing options...
dbo Posted August 24, 2007 Share Posted August 24, 2007 Create a file called testme.php and put this code in it. <?php function IsInteger(&$val) { $matches = preg_match("^\d+$", $val); if( $matches != 0 ) { $val = intval($val); return true; } return false; } ?> <HTML> <HEAD> <TITLE>Testing...</TITLE> </HEAD> <BODY> <?php $test1 = "5"; $test2 = "Doh"; echo $test1 . " is "; $is_int = IsInteger($test1); if( $is_int ) { echo " an integer!<br />"; } else { echo " is NOT an integer!<br />"; } echo "<br /><br />"; echo $test2 . " is "; $is_int = IsInteger($test2); if( $is_int ) { echo " an integer!<br />"; } else { echo " is NOT an integer!<br />"; } ?> </BODY> </HTML> Put this on your webserver and then point your browser to the file. Quote Link to comment https://forums.phpfreaks.com/topic/66430-solved-checking-if-a-variable-is-an-integer/#findComment-332598 Share on other sites More sharing options...
MadTechie Posted August 24, 2007 Share Posted August 24, 2007 $X =$_GET['width']; $isint = (bool)(($X == ((int)$X)) && is_numeric($X)); echo ($isint)?"":"invalid width"; EDIT: leaves it in dbo capable hand Quote Link to comment https://forums.phpfreaks.com/topic/66430-solved-checking-if-a-variable-is-an-integer/#findComment-332600 Share on other sites More sharing options...
dbo Posted August 24, 2007 Share Posted August 24, 2007 Make one modification to the code I previously gave you. Change $matches = preg_match("^\d+$", $val); to $matches = preg_match("^[0-9]+$", $val); Quote Link to comment https://forums.phpfreaks.com/topic/66430-solved-checking-if-a-variable-is-an-integer/#findComment-332601 Share on other sites More sharing options...
Warptweet Posted August 24, 2007 Author Share Posted August 24, 2007 http://warptweet.com/developzone/code.php Doesn't work. Quote Link to comment https://forums.phpfreaks.com/topic/66430-solved-checking-if-a-variable-is-an-integer/#findComment-332603 Share on other sites More sharing options...
MadTechie Posted August 24, 2007 Share Posted August 24, 2007 use $matches = preg_match("/^[0-9]+$/", $val); Quote Link to comment https://forums.phpfreaks.com/topic/66430-solved-checking-if-a-variable-is-an-integer/#findComment-332607 Share on other sites More sharing options...
dbo Posted August 24, 2007 Share Posted August 24, 2007 Correct. Quote Link to comment https://forums.phpfreaks.com/topic/66430-solved-checking-if-a-variable-is-an-integer/#findComment-332608 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.