Code for checking if a string is a number
Posted: Fri Jul 09, 2004 5:01 am
Bech100 | Please use
Code: Select all
tags when posting code. Read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
This code checks whether a string is a number.
U are free to use it if u want...
If anyone got other suggestions, views etc. u could post it here.
Regards toresCode: Select all
<?php
$string = "0123456789";
$number = true;
// PHP4 & PHP5:
$chars = preg_split('//', $string, -1, PREG_SPLIT_NO_EMPTY);
// PHP5:
//$chars = str_split($string);
foreach($chars as $char){
/* Ascii-values: '0' = 48, ... , '9' = 57 */
for($i = 48; $i < 58; $i++){
if(ord($char) == $i){
$number = true;;
break; // break for...
}else{
$number = false;
}
}
if(!$number){
echo $string." is not a number!<br>";
break; // break foreach...
}
}
if($number)
echo $string." is a number!<br>";
?>