ina
ina

Reputation: 19524

PHP - usage of is_numeric() necessary, or can use comparison signs work for all positive numeric cases?

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

Upvotes: 3

Views: 2095

Answers (4)

TCCV
TCCV

Reputation: 3182

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

Upvotes: 5

Artefacto
Artefacto

Reputation: 97805

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.

Upvotes: 0

Dan McGrath
Dan McGrath

Reputation: 42008

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.

Upvotes: 2

Jason McCreary
Jason McCreary

Reputation: 72961

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.

Upvotes: 0

Related Questions