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?
simple number validation
Moderator: General Moderators
-
dimxasnewfrozen
- Forum Commoner
- Posts: 84
- Joined: Fri Oct 30, 2009 1:21 pm
- AbraCadaver
- DevNet Master
- Posts: 2572
- Joined: Mon Feb 24, 2003 10:12 am
- Location: The Republic of Texas
- Contact:
Re: simple number validation
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";
}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.
Re: simple number validation
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.
Re: simple number validation
You mean whether the variable is actually a number type?
Since PHP is loosely-typed I tend to stay away from those.
Code: Select all
if (is_int($number) || is_double($number))