Is_numeric, intval, ctype__digit .. can you rely on them?

is_numeric, intval, ctype__digit .. can you rely on them?

or do i need to use regex?

  function isNum ($ str) { 
return (preg_match ("/ ^ [0-9] + $ /", $ str));
}

what do you guys think? I'm stupid?

+4
source share
7 answers

is_numeric, intval and ctype_digit all do very different things.

is_numeric will tell you if the contents of the variable are numeric (i.e. true if it is a floating point value or an integer).

intval is trying to convert a string of numbers to an integer value

ctype_digit will tell you if the string contains anything other than numeric characters (it will perform the same check as the isNum function).

the best approach is probably to check if is_numeric is true and then use something along the lines of settype ($ myvalue, 'integer') or intval ($ myvalue);

+8
source

The important difference between ctype_digit and is_numerict is a negative value and a floating point number.

is_numeric(-10) will return true , while 'ctype_digit (-10)' will be false

also ctype_digit(12.50) will return false , while is_numeric(12.50) will be true

Thus, both of them are convenient, depending on the context of your domain logic.

+6
source

You named two kinds of functions:

A Validator checks to see if a given value has the specified characteristics and returns either true or false.
is_numeric , ctype_* functions and your isNum function checks the functions, as they simply tell you if the value is valid or not.

A The filter changes the setpoint so that the new value has the specified characteristics and therefore will be valid.
intval and filter_* functions are because they always return valid values ​​that the validator passes.

+3
source

You can also use new features.

 if (!$var = filter_var($var, FILTER_VALIDATE_INT)) { die('Not an int!'); } echo "Var has the value $var.\n"; 

Best used when filtering input from Kli, web client, etc. Filter list here .

+1
source

Follow the docs. is_numeric will always be available and just checks that you have a string that can be considered a PHP number. The ctype_* functions ctype_* bit narrower, but should always be available.

RegEx is, IMO, overkill for such checks.

+1
source

I am not a PHP expert, but have you considered writing unit test data to get a concrete understanding of these functions. If you are unsure of your reliability and the documentation is unclear, then nothing beats the unit test, which can test 1000 permutations and their expected result.

You really don’t even have to go as far as I imagine that you want to test only some special cases. Your programming skills and compiler are your best friend here. The program you are writing will either confirm or refute your suspicions.

In addition, to throw a bonus, you can monitor how much time each method takes and see which is more effective.

Just a thought.

0
source

I'm not sure why you are not just using intval. Your regex doesn't even take negative numbers into account, after all, while intval (although maybe this is what you are going for?).

Or even just casting to int, which avoids some esoteric "gotchas" floating point that might sneak up on intval.

0
source

All Articles