ZeroShade Posted March 9, 2007 Share Posted March 9, 2007 I need to use a conditional statement to check if a number is an integer and that it is also even. I've never used the getType or is_numeric inside a conditional statement before. Can somebody help me out? Link to comment Share on other sites More sharing options...
jlhaslip Posted March 9, 2007 Share Posted March 9, 2007 http://php.net/ Link to comment Share on other sites More sharing options...
ZeroShade Posted March 9, 2007 Author Share Posted March 9, 2007 http://php.net/I've searched the site but I am unable to find as to how to put it into a conditional statement. Link to comment Share on other sites More sharing options...
ZeroShade Posted March 9, 2007 Author Share Posted March 9, 2007 Is it something like this:$number = 5;if(is_numeric($number) AND getType($number))echo "<br />The number ", $number, " is even.";elseecho "<br />The number ", $number, " is odd.";??? Link to comment Share on other sites More sharing options...
jesh Posted March 9, 2007 Share Posted March 9, 2007 I don't know about getType, but you might try the modulus operator (%) which, if you didn't already know, returns the remainder of one number divided into another number. For example, 5 % 2 == 1 because, when you divide 2 into 5, there is a remainder of 1. You can use this to get whether a number is even or not. An odd number % 2 will always return 1 whereas an even number will always return 0. $number = 5if(is_numeric($number) AND ($number % 2 == 0)){ echo "<br />The number ", $number, " is even.";}else{ echo "<br />The number ", $number, " is odd.";} Link to comment Share on other sites More sharing options...
ZeroShade Posted March 9, 2007 Author Share Posted March 9, 2007 I thought the is_numeric() function returns if the number is even or not? Link to comment Share on other sites More sharing options...
Nakor Posted March 9, 2007 Share Posted March 9, 2007 It does. What he is saying is that you can use the modulus operator. The only issue with the code posted by jesh is that it will return saying that the number is odd if you input something that is not a number. You can catch that easily with an else if. Link to comment Share on other sites More sharing options...
pulpfiction Posted March 9, 2007 Share Posted March 9, 2007 is_numeric() finds whether a variable is a number or a numeric stringhttp://us3.php.net/is_numeric Link to comment Share on other sites More sharing options...
pulpfiction Posted March 9, 2007 Share Posted March 9, 2007 Slightly modified..... $number = 5if(is_numeric($number) { if ($number % 2 == 0) { echo "<br />The number ", $number, " is even."; } else { echo "<br />The number ", $number, " is odd."; }else{ echo "Not a Number";} Link to comment Share on other sites More sharing options...
ZeroShade Posted March 9, 2007 Author Share Posted March 9, 2007 Ok... i understand now... thanks!! Link to comment Share on other sites More sharing options...
jesh Posted March 9, 2007 Share Posted March 9, 2007 Slightly modified.....Nice catch... Link to comment Share on other sites More sharing options...
justsomeguy Posted March 11, 2007 Share Posted March 11, 2007 is_numeric is only used to test if a string is a numeric string. It returns true if the string can be parsed as either an integer or a float. If you want to check if your variable is specifically an integer (and not an integer string, btw), you can use this: if(gettype($number) == "integer") { If you want to check if the variable is an integer string, you can use this: if(intval($number) == $number) { Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now