Advertisement
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Numbers between Numbers

Comments in 'Plugin Development' started by Marcelo234, Jan 30, 2016.

  1. Marcelo234
    Offline

    Marcelo234 New Member

    Joined:
    Oct 18, 2014
    Posts:
    204
    Hey Guys....My Plugin update a few things when the player have a few coins...So....how to write that my plugin should do this between two numbers?
    For example:
    If the Player has between 15 and 400 coins something will happen...
    So what is the BETWEEN letter in php?
    Thanks Marcelo234
  2. Dinokiller
    Offline

    Dinokiller Notable Member

    Joined:
    Sep 22, 2013
    Posts:
    376
    Minecraft User:
    Dinokiller_
    PHP:
    if($coins >= 15 && $coins <= 400) { // If amount of coins greater than 15 but less than 400
        // Do stuff
    }
    Last edited: Jan 31, 2016
  3. Dinokiller
    Offline

    Dinokiller Notable Member

    Joined:
    Sep 22, 2013
    Posts:
    376
    Minecraft User:
    Dinokiller_
    I've just edited the code, I accidentally used > and < operators. However I should have used >= and <= instead. Just to explain the difference, the latter checks if the number is equal to 15 or larger than 15 and equal to 400 but larger than 400. If you used the former the condition would not be true if the amount of coins were 15 or 400.
  4. KubixQ
    Offline

    KubixQ Active Member

    Joined:
    Sep 13, 2015
    Posts:
    85
    Minecraft User:
    KubixQ
    Basics, but all people can don't know that. xD
  5. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    "between" can mean exclusively, inclusively or class boundary (min <= x < max). He didn't specify :D

    Also, in PHP 7, say, if you have two boundaries, $a and $b, and you don't want to find out which one is smaller and which one is larger, you can use the spaceship operator:

    PHP:
    $isInside = ($a <=> $x) + ($b <=> $x) === 0;
    If $x is less than or greater than both $a and $b, the sum would be 2 or -2.
    If $x is equal to one of them but not both, the sum would be 1 or -1.
    If $x is equal to both of them, i.e. $a has to be equal to $b, the sum would be 0, since <=> gives out 0 when they are equal.
    If $x is between them, one would resolve to 1 and the other would resolve to -1, so the sum would be 0.

    In conclusion, $isInside would be true if $a=$b=$x (disclaimer: this is not PHP syntax!), or if $x is between $a and $b but not equal to either of them (exclusively between).

    The <=> operator gives 1 if left operand is greater than right, 0 if equal, or -1 if left is less than right.
    Last edited: Feb 2, 2016
    PocketKiller and minebuilder0110 like this.
  6. Dinokiller
    Offline

    Dinokiller Notable Member

    Joined:
    Sep 22, 2013
    Posts:
    376
    Minecraft User:
    Dinokiller_
    Interesting, is the spaceship operator exclusive to PHP or can it be found in other languages?
  7. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder

Share This Page

Advertisement