function integer($str) { if(preg_match('/[^0-9]/', $str)) { return FALSE; } else { return $str; } } Is it possible to create a ternary operator for this statement in PHP?
You should use ctype_digit for that:
function integer($str) { return ctype_digit($str) ? $str : false; } Or use filter_var with FILTER_VALIDATE_INT:
function integer($str) { return filter_var($str, FILTER_VALIDATE_INT, array( 'options' => array('min_range' => 0), 'flags' => FILTER_FLAG_ALLOW_OCTAL )); } filter_var can optionally be configured to allow min and max values, so you could also limit it to positive integers only.min_range only?FILTER_FLAG_ALLOW_OCTAL flag.
is_intfunction: php.net/manual/en/function.is-int.phpis_int("4")returns false.is_numeric