Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save virtualstyle/1e9601287a2c46c040c297e67436e9d7 to your computer and use it in GitHub Desktop.
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.
<?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