Last active
November 11, 2015 19:40
-
-
Save klhall1987/120bd5896971a3e052ef to your computer and use it in GitHub Desktop.
Filter integers and integer strings
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 | |
$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