3

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..

ina
  • 19,167
  • 39
  • 122
  • 201
  • 1
    note - *for `positive values* – ina Jul 23 '10 at 04:12
  • the most common usage here would be for pagination (never negative, usually) - using just a single comparison check that only interprets positive numbers may help with processor time. the above $whatami example with only the comparison check will evaluate to NaN, even if the variable is a string – ina Jul 23 '10 at 04:15
  • PHP has no concept of NaN as other languages do... see my answer. In some math functions, NAN may be returned, but it's not the rule throughout the language. There's even a check for nan: http://php.net/manual/en/function.is-nan.php – TCCV Jul 23 '10 at 04:19
  • well, i guess the question here is a smaller subset - basically validate whether a variable is a *positive number* or not. You'd need is_numeric() for in general, but I have not been able to find a case where `($var<0)` *alone* does not check for if an input is positive number or something other than that. – ina Jul 23 '10 at 04:54

4 Answers4

5

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 string 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).

Community
  • 1
  • 1
TCCV
  • 3,142
  • 4
  • 25
  • 30
  • 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 Jul 23 '10 at 04:16
  • 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?) – TCCV Jul 23 '10 at 04:28
2

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
  • 41,220
  • 11
  • 99
  • 130
  • 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 Jul 23 '10 at 04:18
0

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
  • 71,546
  • 23
  • 135
  • 174
  • it seems that just having the single check `($whatami<0)` would determine if it's a positive number or something else (string, otherwise). – ina Jul 23 '10 at 04:12
0

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
  • 96,375
  • 17
  • 202
  • 225