So I can write a simple function to test that if the value is int. And use array_walk to apply this funciton to the array?
Are there any better simple solutions?
So I can write a simple function to test that if the value is int. And use array_walk to apply this funciton to the array?
Are there any better simple solutions?
If you're not working with multi-dimensional arrays, you could simply do something like this:
function array_is_int($array) {
foreach($array as $value) {
if(!ctype_digit((string)($value))) return FALSE;
}
return TRUE;
}
NOTE: I used [man]ctype_digit/man instead of [man]is_int[/man] since it "uses a native C library and thus processes significantly faster." They should work on any Windows server as long as you have PHP 4.3.0 installed, and any Unix server after version 4.2.0 (assuming they haven't been intentionally disabled).
Keep in mind, though, that for a negative integer ctype_digit((string) $value) will return false.
Hmm... true... maybe [man]is_int[/man] is best here? :p
By the way, general speaking, not necessary for this question. What is the better approach, use the the general set up and more used methods such as is_int() or use the not so popular used function such as ctype_digit?
Use the ctype_digit kind functions may win some performance benefits, but the code could be less understandable for other programmers (the new guy) in the team.
And this performance winning could be easily achieved by a using a better machine or add an additional memory costs only a few hundreds bucks.
I think this debate has been on for a while, one school tries to write the codes to have the best performance. One school tries to write the codes quicker, ignore the small performance issues, as long as there will be no big performance problems, and think the hardware update speed will take care of the performance issue.
searain wrote:Use the ctype_digit kind functions may win some performance benefits, but the code could be less understandable for other programmers (the new guy) in the team.
Basically, you're asking if we should: a) dumb down the code, b) smarten up the 'new guy'. Personally, I would rather challenge the 'new guy' into learning more rather than dragging everyone else down to his level. It would only take the 'new guy' 10 seconds to type in [/b]php.net/ctype_digit to his browser and learn what the function does and, in doing so, increase his understanding of PHP.
I'm sure the performance gain is probably so insignificant that you probably won't need it. I just personally hate it when I'm doing things "just to get by" instead of doing them the best way possible.