Created
January 27, 2025 10:05
-
-
Save Daniel217D/6a7719b7c46f099352d5c14899bc80a6 to your computer and use it in GitHub Desktop.
PHP $is_number isntead of is_numberic
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?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