Q:

Regular Expression for a present & only digits

I need to validate a number in PHP, specifically a DB ID passed in a slug but can't seem to get the regex right...
I can't use is_numeric() because it doesn't filter the numbers I need, same deal with ctype_digit() 
// Values to Test
$tests = [42, '42', '42a', 'a42', '042', 1337, 0, '0', TRUE, FALSE, '1e4', 'not numeric', 9.1, '9.0', ''];

// RegEx
return !preg_match("/[^0-9]/", $val);

It's working mostly but I need to test for a value as well in the RegEx, FALSE and '' are both coming back as valid...
I've tried return !preg_match("/[^0-9]{1}/", $val); to check for a valid length (not value) of 1 but I can't seem to get it right...
I've checked out the samples & comments from from http://davidwalsh.name/php-validatie-numeric-digits but can't get any of the sample code to work...
Does anyone know the RegEx for present and only digits? 

A:

Why double-invert the logic. The easiest you can do is assert that the whole string is made up of digits (1 or more digits to be precise):
return preg_match('/^[0-9]+$/', $val);

or
return preg_match('/^\d+$/', $val);

