SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    Get my greedy down dotJoon's Avatar
    Join Date
    Apr 2003
    Location
    daejeon, South Korea
    Posts
    2,223
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    is it numeric value?

    Code:
    myVariable1=12;
    myVariables2='1a';
    myVariables2='ab';
    I have 3 variables like the above.

    myVariable1 is numeric.
    myVariable2 is half numeric.
    myVariable3 is not numeric.

    I like to check each variable of myVariables is numeric or not numeric.

    The code below doesn't work correctly, but I hope it shows what I want.
    Code:
    if ( $myVariable is numeric )
    
    { 
    echo 'Yes, it's numeric';
    }
    else
    { 
    echo 'No, it's not numeric';
    }

  2. #2
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,806
    Mentioned
    157 Post(s)
    Tagged
    3 Thread(s)
    Have a look at PHP: is_int - Manual
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  3. #3
    Barefoot on the Moon! silver trophy Force Flow's Avatar
    Join Date
    Jul 2003
    Location
    Northeastern USA
    Posts
    4,606
    Mentioned
    56 Post(s)
    Tagged
    1 Thread(s)
    Possibly is_numeric() instead of is_int(): PHP: is_numeric - Manual

    is_int() only checks for integers. is_numeric() checks for ints, decimals/floats, and exponential value notation.
    Visit The Blog | Follow On Twitter
    301tool 1.1.5 - URL redirector & shortener (PHP/MySQL)
    Can be hosted on and utilize your own domain

  4. #4
    SitePoint Wizard tgavin's Avatar
    Join Date
    Feb 2003
    Location
    FL
    Posts
    1,051
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Like Force Flow said. is_numeric() is boolean, so it returns true if numeric and false if not numeric.

    PHP Code:
    if ( is_numeric($myVariable) )


    echo 
    "Yes, it's numeric";
    }
    else

    echo 
    "No, it's not numeric";


  5. #5
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Building on Mikes' hint that you look at using (int) to typecast to an integer, look at these examples:

    PHP Code:
    $myVar[0]=12;  // an int
    $myVar[1]='1a'// a string starting with an int
    $myVar[2]='ab'// a string with no ints
    $myVar[3]='12'// a string containing ints
    $myVar[4]='a1'// a string containing an int but starting with a non-int
    $myVar[5]='1.8'// a string containing a float

    foreach( $myVar as $v)
    echo (int)
    $v '<br>';
    // gives
    12
    1
    0
    12

    1  
    // nb does not round up 
    Depending on what you want to do with the likes of "1a", where PHP tries to analyse it as a number, finds a 1, thats OK, then finds an a, so drops it.

    Whereas if the string starts with a non-numeric char "a1" it gives up and returns 0.

    typical use:
    PHP Code:
    if( (int)$var === 0) {
    // failed the int test - send away
    }else{
    // passed the int test - get on processing

    I use this a lot when I am expecting, say, a numerical id key to be passed from a form element. Short and sweet, and therefore memorable .

    So you have got to be very clear in your mind what you want to happen in your middle test "1a"


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •