IF Statement for Numbers

Discussion in 'PHP' started by diy-day, Jun 26, 2012.

  1. #1
    Hello, I'm looking for a conditional statement that checks a field entry for the presence of any digit (0 - 9). Along the lines of:

    if ($variable {[I]where I'm lost[/I]})
    Code (markup):
    I appreciate any help in this matter...
     
    diy-day, Jun 26, 2012 IP
  2. NetStar

    NetStar Notable Member

    Messages:
    2,471
    Likes Received:
    541
    Best Answers:
    21
    Trophy Points:
    245
    #2
    You could of read a doc. You could of used google. There's so many things you could of done BEFORE asking for help.

    If you're looking to verify if a value is in numeric form use is_numeric() like so:

    
    $blah = 25;
    
    
    if (is_numeric($blah))
    {
      print "Yep..it's a number\n";
    } else
    {
       print "Not a number!\n";
    }
    
    
    
    PHP:

    If you just want to see if a number exists IN a string you could use a simple regex: preg_match("/[0-9]/", $blah)
     
    NetStar, Jun 26, 2012 IP
  3. diy-day

    diy-day Peon

    Messages:
    95
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Don't presume to know what I did before I started the topic. Yes, I could "of" done those things. I did those things, but I didn't find what I was looking for.

    If I didn't do my research before starting a topic, as far as coding was concerned, I'd be posting here more often...
     
    diy-day, Jun 26, 2012 IP
  4. NetStar

    NetStar Notable Member

    Messages:
    2,471
    Likes Received:
    541
    Best Answers:
    21
    Trophy Points:
    245
    #4
    I didn't "presume". I actually knew you didn't bother to try to find the answer your on own. A simple Google search for "PHP Numbers Only" or "PHP Numeric Values" or "PHP is_numeric()" would of returned your answer. A simple trip to PHP.net and a 10 second glance under numeric functions would of also returned your answer. You didn't check.

    But regardless...was your question not answered?
     
    NetStar, Jun 26, 2012 IP
  5. diy-day

    diy-day Peon

    Messages:
    95
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Thanks, Carnac the Magnificent, now you're telling me what I did and didn't do...I may not have used those exact search terms, so I didn't find the results you found. But, then again, it didn't occur to me to use those exact terms. And since you're such a mind reader, you'd know that I've been to that site many times, and I still couldn't find what I was looking for. Again, I probably didn't know the best place to look...

    Your first solution is absolutely NOT what I'm looking for.
    Tell me how/if you can include your 2nd solution in an IF condition, and I'll let you know if it's what I'm looking for...
     
    diy-day, Jun 26, 2012 IP
  6. NetStar

    NetStar Notable Member

    Messages:
    2,471
    Likes Received:
    541
    Best Answers:
    21
    Trophy Points:
    245
    #6
    Why would I after you insulted the only person who helped you? Figure it out yourself. The answer is there and should be quite obvious.
     
    NetStar, Jun 26, 2012 IP
  7. diy-day

    diy-day Peon

    Messages:
    95
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    So you didn't do your research either...:rolleyes:. Word of advice, next time, come in with a better attitude and at least imagine that the question poser did some research on his/her own. Especially if that person doesn't usually ask that type of question.

    Well, don't you worry about it anymore, I can always seek assistance from someone who doesn't make assumptions...
     
    diy-day, Jun 26, 2012 IP
  8. NetStar

    NetStar Notable Member

    Messages:
    2,471
    Likes Received:
    541
    Best Answers:
    21
    Trophy Points:
    245
    #8
    I gave you 2 options. The second one is to detect if a number is with in a string. All you had to do was swap the functions. See below:

    
    $blah = "Test to see if the number 25 will be detected";
    
    if (preg_match("/[0-9]/", $blah))
    {
      print "blah contains a number in it\n";
    } else
    {
       print "blah does not contain a number in it!\n";
    }
    
    
    PHP:
     
    NetStar, Jun 26, 2012 IP
  9. mcfc4eva

    mcfc4eva Well-Known Member

    Messages:
    602
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    110
    #9
    Guys, come on! That argument was so petty and unneeded. diy-day, did NetStar answer your question?
     
    mcfc4eva, Jun 26, 2012 IP