Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- $tests = array(
- '1', 1, '1.4', 1.4, 'a', '1e4', array()
- );
- foreach($tests as $test) {
- var_dump(
- $test,
- (bool)preg_match('#^[0-9]+$#', $test),
- is_numeric($test) && $test == (int)$test
- );
- echo "\r\n";
- }
- $start = microtime();
- for($i=0; $i < 100;$i++) {
- foreach($tests as $test) {
- is_numeric($test) && $test == (int)$test;
- }
- }
- echo microtime() - $start."\r\n";
- $start = microtime();
- for($i=0; $i < 100;$i++) {
- foreach($tests as $test) {
- preg_match('#^[0-9]+$#', $test);
- }
- }
- echo microtime() - $start."\r\n";
- /*
- string(1) "1"
- bool(true)
- bool(true)
- int(1)
- bool(true)
- bool(true)
- string(3) "1.4"
- bool(false)
- bool(false)
- float(1.4)
- bool(false)
- bool(false)
- string(1) "a"
- bool(false)
- bool(false)
- string(3) "1e4"
- bool(false)
- bool(false)
- array(0) {
- }
- bool(false)
- bool(false)
- 0.00048
- 0.053274
- */
- ?>
Advertisement
Add Comment
Please, Sign In to add comment