InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

Menu
  • Home
  • Sitemap

Python Programming Language Best Tutorials and Code Examples

Learn Python Right Now!
Home
PHP
Checking Whether a Variable Contains a Valid Number in PHP
PHP

Checking Whether a Variable Contains a Valid Number in PHP

InfinityCoder November 15, 2016

You want to ensure that a variable contains a number, even if it’s typed as a string. Alternatively, you want to check if a variable is not only a number, but is also specifically typed as one.

Use is_numeric() to discover whether a variable contains a number:

1
2
3
4
5
6
7
8
9
10
11
foreach ([5, '5', '05', 12.3, '16.7', 'five', 0xDECAFBAD, '10e200'] as $maybeNumber) {
    $isItNumeric = is_numeric($maybeNumber);
    $actualType = gettype($maybeNumber);
    print "Is the $actualType $maybeNumber numeric? ";
    if (is_numeric($maybeNumber)) {
        print "yes";
    } else {
        print "no";
    }
    print "\n";
}

The example code prints:

1
2
3
4
5
6
7
8
Is the integer 5 numeric? yes
Is the string 5 numeric? yes
Is the string 05 numeric? yes
Is the double 12.3 numeric? yes
Is the string 16.7 numeric? yes
Is the string five numeric? no
Is the integer 3737844653 numeric? yes
Is the string 10e200 numeric? yes

Numbers come in all shapes and sizes. You cannot assume that something is a number simply because it only contains the characters 0 through 9.

What about decimal points, or negative signs? You can’t simply add them into the mix because the negative must come at the front, and you can only have one decimal point. And then there’s hexadecimal numbers and scientific notation.

Instead of rolling your own function, use is_numeric() to check whether a variable holds something that’s either an actual number (as in it’s typed as an integer or floating point), or a string containing characters that can be translated into a number.

There’s an actual difference here. Technically, the integer 5 and the string 5 aren’t the same in PHP. However, most of the time you won’t actually be concerned about the distinction, which is why the behavior of is_numeric() is useful.

Helpfully, is_numeric() properly parses decimal numbers, such as 5.1; however, numbers with thousands separators, such as 5,100, cause is_numeric() to return false.

To strip the thousands separators from your number before calling is_numeric(), use str_replace():

1
2
3
4
5
6
7
$number = "5,100";
 
// This is_numeric() call returns false
$withCommas = is_numeric($number);
 
// This is_numeric() call returns true
$withoutCommas = is_numeric(str_replace(',', '', $number));

To check if your number is a specific type, there are a variety of related functions with self-explanatory names: is_float() (or is_double() or is_real(); they’re all the same) and is_int() (or is_integer() or is_long()).

Share
Tweet
Email
Next Article

Related Articles

Converting Between Bases in PHP
You need to convert a number from one base to …

Converting Between Bases in PHP

Using an Accelerator in PHP
You want to increase performance of your PHP applications. Use …

Using an Accelerator in PHP

About The Author

InfinityCoder
InfinityCoder

Leave a Reply

Cancel reply

Recent Tutorials InfinityQuest

  • Adding New Features to bash Using Loadable Built-ins in bash
    Adding New Features to bash Using Loadable …
    June 27, 2017 0
  • Getting to the Bottom of Things in bash
    Getting to the Bottom of Things in …
    June 27, 2017 0

Recent Comments

  • fer on Turning a Dictionary into XML in Python
  • mahesh on Turning a Dictionary into XML in Python

Categories

  • Bash
  • PHP
  • Python
  • Uncategorized

InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

About Us

Start learning your desired programming language with InfinityQuest.com.

On our website you can access any tutorial that you want with video and code examples.

We are very happy and honored that InfinityQuest.com has been listed as a recommended learning website for students.

Popular Tags

binary data python CIDR convert string into datetime python create xml from dict python dictionary into xml python how to create xml with dict in Python how to write binary data in Python IP Address read binary data python tutorial string as date object python string to datetime python

Archives

  • June 2017
  • April 2017
  • February 2017
  • January 2017
  • December 2016
  • November 2016
Copyright © 2021 InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more
Programming Tutorials | Sitemap