isnumeric function for PHP

Posted on 2006-03-28
Last Modified: 2013-12-13
Hi is there an isnumeric or similar function available for PHP?

Values could be any numeric type just need to check that the string value doesn't actually contain characters.

Question by:gaz1000
    LVL 49

    Expert Comment


    LVL 40

    Accepted Solution

    This is from the PHP Manual ...

    bool is_numeric ( mixed var )

    Finds whether the given variable is numeric. Numeric strings consist of optional sign, any number of digits, optional decimal part and optional exponential part. Thus +0123.45e6 is a valid numeric value. Hexadecimal notation (0xFF) is allowed too but only without sign, decimal and exponential part.

    There is also is_int() and is_float()

    Notes about is_int() and is_float() though ...

    To test if a variable is a number or a numeric string (such as form input, which is always a string), you must use is_numeric().

    None of these functions return the numeric value, they just tell you that it IS a number/int/float.

    Depending upon the exact format of the number (i.e. thousand separators, comma decimal place, etc), you will have problems ...

    $as_test_strings = array
          '100', // Normal integer
          '3.1415', // Float
          '1,000,000', // With thousand separator
          '1.234.567,89', // With thousand separator and French , and .

    foreach($as_test_strings as $s_test_string)
          echo $s_test_string . ' is ' . (is_numeric($s_test_string) ? '' : 'not ') . "numeric\n";

    reports ...

    100 is numeric
    3.1415 is numeric
    1,000,000 is not numeric
    1.234.567,89 is not numeric

    The French format is documented with an example in the PHP Function number_format().

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
    Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
    Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
    The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now