PHP Numbers Checking Whether a Variable Contains a Valid Number - Web Development and Design | Tutorial for Java, PHP, HTML, Javascript PHP Numbers Checking Whether a Variable Contains a Valid Number - Web Development and Design | Tutorial for Java, PHP, HTML, Javascript

Breaking

Post Top Ad

Post Top Ad

Wednesday, May 8, 2019

PHP Numbers Checking Whether a Variable Contains a Valid Number

PHP Numbers





Checking Whether a Variable Contains a Valid Number


Problem

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.


Solution

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

               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:               

               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


Discussion

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 thesame 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():

               $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()).


No comments:

Post a Comment

Post Top Ad