You should use ctype_digit for that:
function integer($str) { return ctype_digit($str) ? $str : false; } Or use filter_var with FILTER_VALIDATE_INTFILTER_VALIDATE_INT:
function integer($str) { return filter_var($str, FILTER_VALIDATE_INT, array( 'options' => array('min_range' => 0), 'flags' => FILTER_FLAG_ALLOW_OCTAL )); }