simple number validation

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
dimxasnewfrozen
Forum Commoner
Posts: 84
Joined: Fri Oct 30, 2009 1:21 pm

simple number validation

Post by dimxasnewfrozen »

I'm trying to validate if a variable is a number or not

I'm using the is_numeric() function but it sees a set of numbers a string for example..
12345567

Is there another function I can use to check if the variable is a number?
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: simple number validation

Post by AbraCadaver »

Code: Select all

$string = '12345567';
$number = 12345567;
 
if(is_numeric($string)) {
    echo "THIS IS A NUMBER\n";
}
 
if(is_numeric($number)) {
    echo "THIS IS A NUMBER\n";
}
Output is:

THIS IS A NUMBER
THIS IS A NUMBER

What is the problem?
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
MichaelR
Forum Contributor
Posts: 148
Joined: Sat Jan 03, 2009 3:27 pm

Re: simple number validation

Post by MichaelR »

Code: Select all

 
 
$string = '12345';
$number =  12345;
 
if (is_int($string) || is_float($string)) {
  echo '$string is a number.' . PHP_EOL;
}
 
if (is_int($number) || is_float($number)) {
  echo '$number is a number.' . PHP_EOL;
}
 
// Outputs: $number is a number.
 
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: simple number validation

Post by requinix »

You mean whether the variable is actually a number type?

Code: Select all

if (is_int($number) || is_double($number))
Since PHP is loosely-typed I tend to stay away from those.
Post Reply