ted_chou12 Posted June 1, 2009 Share Posted June 1, 2009 Hi, I can't seem to get this right if (!is_int($guestno)) {echo "it is not an integer!"} it doesn't work, ??? Thanks, Ted Quote Link to comment https://forums.phpfreaks.com/topic/160434-solved-is_int-false-condition/ Share on other sites More sharing options...
Ken2k7 Posted June 1, 2009 Share Posted June 1, 2009 Just for future reference, posting "it doesn't work" without explaining in what way it doesn't work doesn't help us help you. Is $guestno an integer value. By that, I mean it is a int, not string, even if the string holds a numeric value. You may want is_numeric? Or you can just use intval to cast it as an integer. Quote Link to comment https://forums.phpfreaks.com/topic/160434-solved-is_int-false-condition/#findComment-846643 Share on other sites More sharing options...
ted_chou12 Posted June 1, 2009 Author Share Posted June 1, 2009 sorry for cutting the details, by it doesnt work, i mean for both "1" and "1a" the result is false. Thanks Just to add on, i don't want intval(), i just want to check if the value is interger or not, and i replaced is_int() with is_numeric, it worked, but i have no clue why is_int doesn't work, i strongly believe it's a bug. Quote Link to comment https://forums.phpfreaks.com/topic/160434-solved-is_int-false-condition/#findComment-846647 Share on other sites More sharing options...
Ken2k7 Posted June 1, 2009 Share Posted June 1, 2009 Because '1' is a string, not a number. You should learn to read the manual on is_int? Like I said in my previous post, use is_numeric, intval or just cast it as an int. Quote Link to comment https://forums.phpfreaks.com/topic/160434-solved-is_int-false-condition/#findComment-846649 Share on other sites More sharing options...
MadTechie Posted June 1, 2009 Share Posted June 1, 2009 Thats because both are strings "1" = string 1 = int Quote Link to comment https://forums.phpfreaks.com/topic/160434-solved-is_int-false-condition/#findComment-846650 Share on other sites More sharing options...
ted_chou12 Posted June 1, 2009 Author Share Posted June 1, 2009 I've used "" in this context to show the numbers as examples, but in the code, they are actual numbers, and the situation worked for is_numeric, so i dont see why the is_int doesn't work with it. Thanks, Quote Link to comment https://forums.phpfreaks.com/topic/160434-solved-is_int-false-condition/#findComment-846655 Share on other sites More sharing options...
Ken2k7 Posted June 1, 2009 Share Posted June 1, 2009 Do this for me - var_dump($guestno); Put that before the if statement you had up there. What does that print out? Quote Link to comment https://forums.phpfreaks.com/topic/160434-solved-is_int-false-condition/#findComment-846657 Share on other sites More sharing options...
ted_chou12 Posted June 1, 2009 Author Share Posted June 1, 2009 string(1) "1", i guess u are right, then what should I use in this situation to test out if the input value is an integer? <form name="addorder" action="" method="post"> <input name="guestno" value="<?php echo $guestno;?>" type="text" size="30" maxlength="2" /><br /> <?php if (isset($_POST['submit'])){ $guestno = $_POST['guestno']; if (!is_int($guestno)) {echo "<p><b>Guest number field must be in integers!<b></p>"; $invalid = true;}?> Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/160434-solved-is_int-false-condition/#findComment-846659 Share on other sites More sharing options...
Ken2k7 Posted June 1, 2009 Share Posted June 1, 2009 $_POST values are always strings, just so you know. And have you read a thing I said in my first 2 posts. Here's intval $guestno = intval($_POST['guestno']); Quote Link to comment https://forums.phpfreaks.com/topic/160434-solved-is_int-false-condition/#findComment-846662 Share on other sites More sharing options...
ted_chou12 Posted June 1, 2009 Author Share Posted June 1, 2009 $_POST values are always strings, just so you know. And have you read a thing I said in my first 2 posts. Here's intval $guestno = intval($_POST['guestno']); ive read the manual about intval, it does fix the string to make it an integer, but it doesn't check if the input is an integer, that's what i don't like about the function. is there a way to check instead of changing? Thanks again Quote Link to comment https://forums.phpfreaks.com/topic/160434-solved-is_int-false-condition/#findComment-846665 Share on other sites More sharing options...
Ken2k7 Posted June 1, 2009 Share Posted June 1, 2009 Try this: if (preg_match('#^\d$#', $guestno)) Quote Link to comment https://forums.phpfreaks.com/topic/160434-solved-is_int-false-condition/#findComment-846666 Share on other sites More sharing options...
ted_chou12 Posted June 1, 2009 Author Share Posted June 1, 2009 okay, thanks! Quote Link to comment https://forums.phpfreaks.com/topic/160434-solved-is_int-false-condition/#findComment-846667 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.