mkosmosports Posted January 20, 2007 Share Posted January 20, 2007 How can I write a condition, if $variable is a digit? I have numbers and words in a table. Is there any simple way to write that condition?Thanks. Quote Link to comment Share on other sites More sharing options...
Jessica Posted January 20, 2007 Share Posted January 20, 2007 if(intval($var)){}Or you could use is-numeric()http://us3.php.net/manual/en/function.is-numeric.php Quote Link to comment Share on other sites More sharing options...
tauchai83 Posted January 20, 2007 Share Posted January 20, 2007 yes..tat will definitely work! Quote Link to comment Share on other sites More sharing options...
ShogunWarrior Posted January 20, 2007 Share Posted January 20, 2007 You could alternatively use:[code]$input = '88';$match = preg_match("/^[0-9]+$/",$input);if( $match ){ //wow, it is a number}[/code]This version will check that it is a single digit (0-9):[code]$input = '88';$match = preg_match("/^[0-9]{1}$/",$input);if( $match ){ //wow, it is a single digit}[/code]Or to simply check it is a number:[code]if( is_numeric( $input ) ){}[/code] Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.