Skip to content

Instantly share code, notes, and snippets.

@klhall1987
Last active November 11, 2015 19:40
Show Gist options
  • Save klhall1987/120bd5896971a3e052ef to your computer and use it in GitHub Desktop.
Save klhall1987/120bd5896971a3e052ef to your computer and use it in GitHub Desktop.
Filter integers and integer strings
<?php
$numbers = array(
12,
1551,
'21',
'5115',
'I am a string',
);
//set to loop over the numbers array.
foreach( $numbers as $number ) {
//check to see if the value is either an integer or a numeric string.
if( is_numeric( $number ) ) {
//filters integers
if( is_int( $number) ) {
echo $number . ' is an integer.' . '</br>';
//filters numeric strings
} elseif ( intval( $number ) ) {
echo $number . ' is a numeric string, please convert to an integer.' . '</br>';
}
}
}
?>
****OUTPUT****
12 is an integer.
1551 is an integer.
21 is a string please convert to an integer.
5115 is a string please convert to an integer.
**************
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment