Alex Rivera | Logout

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

Asked 2009-01-20T18:14:36.197
10

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

or do i have to use regex?

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

what do you guys think? am i stupid?

Edit
Report

1 Answer

0

I am not an expert on PHP, but have you considered writing a data driven unit test to gain a concrete understanding of those functions. If you are uncertain about their reliability and the documentation is unclear, then nothing beats a unit test that can test 1000's of permutations and their expected output.

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

Also, to throw in a bonus you can monitor how long each method takes and see which is more performant.

Just a thought.

answered 2009-01-20T18:21:44.887

Your Answer