www.webdeveloper.com
Results 1 to 8 of 8

Thread: PHP IF integer

  1. #1
    Join Date
    Jul 2009
    Posts
    16

    PHP IF integer

    I'm trying to make a form which searches for a product by its ID number but also performs a keyword search (in the same form).

    I've got these seperately at the moment... the idea is to create an IF statement.

    Is there a way to make an IF statement look for integers?

    The idea would work like:

    IF integer
    use input 1
    else use input 2

    Thanks.


    *** Or should this be done in VBScript rather than PHP? ***
    Last edited by little-persia; 10-22-2009 at 09:20 AM. Reason: Added question

  2. #2
    Join Date
    Jan 2007
    Location
    Wisconsin
    Posts
    2,120
    Jon Wire

    thepointless.com | rounded corner generator

    I agree with Apple. Flash is just terrible.

    Use CODE tags!

  3. #3
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,427
    PHP Code:
    if( (int) $a == $a)
    {
       
    // use $a
    }
    else
    {
       
    // use something else

    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  4. #4
    Join Date
    Nov 2008
    Posts
    2,477
    I may be wrong but I think the OP might mean something more like this:

    PHP Code:
    if (is_int($a)) {
        
    //-->
    } else {
        
    //-->

    or possibly

    PHP Code:
    if (is_numeric($a)) {
        
    //-->
    } else {
        
    //-->


  5. #5
    Join Date
    Jan 2007
    Location
    Wisconsin
    Posts
    2,120
    ahh ... touche.
    Jon Wire

    thepointless.com | rounded corner generator

    I agree with Apple. Flash is just terrible.

    Use CODE tags!

  6. #6
    Join Date
    Jul 2009
    Posts
    16
    Thanks,

    Will this work with a form?

    I've trying to put one <input...> if the user enters a number and another <input...> if the user enters a string.

    Basically I'm looking for it to search for an item reference number if they put in a reference number or search for keywords if they put in a text string.

  7. #7
    Join Date
    Nov 2008
    Posts
    2,477
    You will probably either want to use a regular expression, or if it's a simple number then is_numeric. Bear in mind that since HTTP essentially uses a long string of text, any variable you receive in your PHP script is actually going to be a string type, so is_numeric should be used over is_int.

  8. #8
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,427
    is_int() will not work with something like form inputs, as they are all strings. is_numeric() would "work", except "999.999" is numeric but not an integer. ctype_digit() would work as long as you realize that something like "9999999999999999999" would pass, but it could not be used as a "normal" signed 32-bit integer (but could be used with the BCMath functions as an integer). It all depends on what the ultimate requirements are. If you just want to ensure that the value consists only of the digits 0-9, ctype_digit() would be my preference. If you want to ensure that it is usable as a standard integer on your installation, I would go with the cast-to-integer option I suggested above.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles