1. The forums will be archived and moved to a read only mode in about 2 weeks (mid march).

Solved How I can make a command only accept numbers ?

Discussion in 'Development' started by Groin, May 14, 2017.

  1. Groin

    Groin Baby Zombie

    Messages:
    159
    Ex: /add <number 1> <number 2>
    If number < 10 if will doesn't work...
    If number > 16 if will doesn't work neither...
     
  2. corytortoise

    corytortoise Skeleton

    Messages:
    825
    GitHub:
    corytortoise
    You'll have to be a bit more descriptive, but I believe you can accomplish this by learning "if statements" in PHP first. Use the internet for that, this isn't the place for general PHP questions.
     
    HimbeersaftLP and jasonwynn10 like this.
  3. jasonwynn10

    jasonwynn10 Moderator Poggit Reviewer

    Messages:
    1,489
    GitHub:
    jasonwynn10
    1. please show proof of a previous attempt
    2. please learn how to program on your own before asking a forum about PocketMine API about generic things
    3. This needs moved to facepalm...
     
    archie426 likes this.
  4. HimbeersaftLP

    HimbeersaftLP Fish

    Messages:
    2,402
    GitHub:
    HimbeersaftLP
    Have even TRIED searching before you posted this?
     
    jasonwynn10 and Teamblocket like this.
  5. SkySeven

    SkySeven Baby Zombie

    Messages:
    145
    GitHub:
    SkySevenMC
    With is_numeric ?
    PHP:
    if(is_numeric($number)){
    //Your code
    }
     
  6. Lowkey

    Lowkey Slime

    Messages:
    94
    Better to use if_int to check if its an integer, as you probably dont want to be working with complex floats (decimals).
     
  7. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    If you mean only using is_int, this is probably wrong.
    Screenshot_2017-05-16-09-13-04.png
    is_int only checks the variable type to be an int. It does not check for a numeric string to represent integer, nor float numbers to represent an integer.
     
  8. jasonwynn10

    jasonwynn10 Moderator Poggit Reviewer

    Messages:
    1,489
    GitHub:
    jasonwynn10
    so... type casting?
     
  9. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    If you cast it to an int, it must be an int, so it is pointless to check anymore.
    Use ctype_digit() to make sure all numbers are numeric if you don't want to allow decimal or negative. You may also ternarily check the first digit to be a negative sign, and conditionally substr(, 1) it.
     
    jasonwynn10 likes this.
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.