Created
May 28, 2016 00:46
-
-
Save virtualstyle/1e9601287a2c46c040c297e67436e9d7 to your computer and use it in GitHub Desktop.
This gist is an answer to the Programming Challenge: "Determine if a String is an int or a double." for a job application.
This file contains 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 | |
function intOrDouble( $str ) { | |
if ( is_numeric( $str ) ) { | |
if ( strpos($str, ".") !== false ) { | |
return 1; | |
} else { | |
return 0; | |
} | |
} else { | |
return -1; | |
} | |
} | |
$tests = array( 'test', '2', '004', '6.73', '0.12', 'zero.three' ); | |
$results = array( -1 => 'neither int nor double', 0 => 'int', 1 => 'double' ); | |
echo '<pre>'; | |
print_r( $tests ); | |
echo '</pre>'; | |
foreach ( $tests as $key => $test ) { | |
echo '$tests[' . $key .'] = ' . $results[ intOrDouble ( $test ) ] .'<br>'; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment