views:

53

answers:

4

It seems that simple comparison signs >,>= and their reverse components can evaluate if a certain variable is a number or not. Example $whatami='beast'; ($whatami<0)?echo 'NaN':echo 'is numeric!';

Are there cases where is_numeric() usage is necessary for positive values (number >0)? It seems that using comparison signs above would determine if the variable is numeric..

+2  A: 

There is a big difference between "can evaluate if a certain variable is a number or not" and "evaluate if a certain variable is a positive number". Using the comparison signs require you to test it twice (Both > & <= or >= & <) and may not be immediately obvious. is_numeric means you only need a single test and makes it quite obvious what you are doing.

Also, a string will evaluate as 0, meaning it throws your idea out. Stick with the proper commands :)

As per comment: Well, in this case, you are asking for comparing is_numeric against a test for positive numbers, excluding 0. This is not the intent for is_numeric, so naturally it may not be necessary. If you do a mathematical check that involves 0 as the answer or as part of the range, you will need is_numeric, otherwise you won't need it. The first part of your question asks a different question, so:

It seems that simple comparison signs >,>= and their reverse components can evaluate if a certain variable is a number or not - Incorrect

Are there cases where is_numeric() usage is necessary for positive values (number >0)? - No

It seems that using comparison signs above would determine if the variable is numeric - No. They can determine if a variable is either a non-zero number or unknown, not numeric.

Dan McGrath
evaluating as 0 is ok, since only positive numeric values are taken. (0 is thrown out!) - sorry, should have put the *positive numeric values* in bold above - just did
ina
A: 

Comparison will depend on the type of data on the left side of the operator.

The important thing to remember is that PHP is not a strongly typed language. If you want to compare a number and ensure it is a number, then yes, is_numeric() would be a good check. For example,

echo (is_numeric($whatami) && $whatami < 0) ? 'number greater than zero' : 'NaN or negative';

However, this shouldn't be generalized. If you can comment more on what you are wanting to do, you may find a more detailed answer.

Jason McCreary
it seems that just having the single check `($whatami<0)` would determine if it's a positive number or something else (string, otherwise).
ina
+3  A: 

As I have been finding out, a lot of these helper functions are really necessary because PHP isn't strongly typed. I posted a similar question (although not that similar) about isset earlier this week. One thing to note is that PHP will change your sting to its integer value for comparisons during some instances (when there are mixed types). This can't be overlooked. I think this is a strong case for is_numeric

from PHP Manual

If you compare a number with a string or the comparison involves numerical strings, then each string is converted to a number and the comparison performed numerically. These rules also apply to the switch statement. The type conversion does not take place when the comparison is === or !== as this involves comparing the type as well as the value.

Another thing to think about is that "what is 0" in PHP. It means a lot. It's not always numeric. It may be a numeric string, boolean false, integer, etc... This is why those helper functions exist.

To add to my answer:

change your example:

$whatami='beast';  
($whatami<5) ? echo 'less than 5' : echo 'more than 5';

PHP would change 'beast' to its integer equivalent and then do the comparison. This would give unintended results. If you really wanted something similar, you'd have to wrap it in another conditional:

$whatami='beauty';  
if(is_numeric($whatami){
    ($whatami<5) ? echo 'less than 5' : echo 'more than 5';
} else {
    exit('what, am I not pretty enough for a beast?');
}

Then you would get your intended result (as weird as it may be).

Tim
OK - sorry, I meant to say it's only a check for positive numeric values. (numbers > 0) being the subset of validated variables to take. See comments above!
ina
if you already validated or type-cast the variable, and you are certain that it hasn't changed, then you can safely leave out is_numeric for comparisons or absolutely know that the value is numeric. This (kind of) assumes that you already used is_numeric in your code, though. You may still want to use it because if you don't then something could sneak up on you (if you get the number from a DB that's always an INT, what happens when the query fails or returns NULL?)
Tim
A: 

Yes, there are cases.

For instance:

var_dump("5aa" > 4); //bool(true)
var_dump("5aa" > 6); //bool(false)

As you can see, the conversion of "5aa" to int(5). Let's see what is_numeric gives:

var_dump(is_numeric("5aa")); //bool(false)

So, is_numeric is more strict. Whether it's necessary depends on your application.

Notice that are cases where a numeric string and a number are not exactly the same thing:

var_dump("255" & "2"); //string(1) "2" 
var_dump(255 & 2); //int(2)

See bitwise operations:

Be aware of data type conversions. If both the left-hand and right-hand parameters are strings, the bitwise operator will operate on the characters' ASCII values.

Artefacto