Jump to content

is_numeric and getType


ZeroShade

Recommended Posts

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

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

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

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
×
×
  • Create New...