Skip to content

Instantly share code, notes, and snippets.

@Daniel217D
Created January 27, 2025 10:05
Show Gist options
  • Select an option

  • Save Daniel217D/6a7719b7c46f099352d5c14899bc80a6 to your computer and use it in GitHub Desktop.

Select an option

Save Daniel217D/6a7719b7c46f099352d5c14899bc80a6 to your computer and use it in GitHub Desktop.
PHP $is_number isntead of is_numberic
<?php
declare( strict_types=1 );
$tests = array(
"42",
1337,
0x539,
02471,
0b10100111001,
1337e0,
"0x539",
"02471",
"0b10100111001",
"1337e0",
"not numeric",
array(),
9.1,
null,
'',
);
foreach ($tests as $element) {
if (is_numeric($element)) {
echo var_export($element, true) . " is numeric", PHP_EOL;
} else {
echo var_export($element, true) . " is NOT numeric", PHP_EOL;
}
}
$is_number = static function ($value):bool {
return is_numeric( $value ) && (string) $value === (string) (float) $value;
};
var_dump('------');
foreach ($tests as $element) {
if ($is_number($element)) {
echo var_export($element, true) . " is numeric", PHP_EOL;
} else {
echo var_export($element, true) . " is NOT numeric", PHP_EOL;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment