Wednesday, 15 June 2011

PHP code to check if array is numeric is not working -



PHP code to check if array is numeric is not working -

i have next php:

<?php $array = array("1","2","3"); $only_integers === array_filter($array,'is_numeric'); // true if($only_integers == true) { echo 'right'; } ?>

for reason returns nothing. don't know i'm doing wrong.

thanks

is_int checks actual type of variable, string in case. utilize is_numeric numeric values regardless of variable type.

note next values considered "numeric":

"1" 1 1.5 "1.5" "0xf" "1e4"

i.e. floats, integers or strings valid representations of floats or integers.

edit: also, might have misunderstood array_filter, not homecoming true or false new array values callback function returned true. if($only_integers) works nonetheless (after fixed assignment operator) because non-empty arrays considered "true-ish".

edit 2: @sdc pointed out, should utilize ctype_digit if want allow integers in decimal format.

php arrays function integer numeric

No comments:

Post a Comment