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

What is the isset function?

Comments in 'Plugin Development' started by RekkuzaRage, May 5, 2015.

  1. RekkuzaRage
    Offline

    RekkuzaRage Active Member

    Joined:
    Apr 15, 2014
    Posts:
    251
    Minecraft User:
    RekkuzaRage
    I been looking at plugin's source codes and be seeing the function isset function multiple times, I was not really sure what it was. So I looked it up, I didn't really understand it. What does it mean? Also how can this function be used in PocketMine?
    Hotshot_9930 likes this.
  2. Falk
    Offline

    Falk Staff Member Global Moderator

    Joined:
    Sep 2, 2013
    Posts:
    1,707
    Plugins:
    22
    Minecraft User:
    Falkirknh
    isset returns true if the variable (or variables) passed to it is assigned. If a variable is assigned, it means that it contains some information.
    iJoshuaHD and Hotshot_9930 like this.
  3. RekkuzaRage
    Offline

    RekkuzaRage Active Member

    Joined:
    Apr 15, 2014
    Posts:
    251
    Minecraft User:
    RekkuzaRage
    So if I have a variable named $chocolate and equals a integer, EX: $chocolate = 7;
    It will return true?
    Hotshot_9930 likes this.
  4. Falk
    Offline

    Falk Staff Member Global Moderator

    Joined:
    Sep 2, 2013
    Posts:
    1,707
    Plugins:
    22
    Minecraft User:
    Falkirknh
    Yep.
    RekkuzaRage and Hotshot_9930 like this.
  5. Hotshot_9930
    Offline

    Hotshot_9930 Notable Member Plugin Developer

    Joined:
    May 26, 2014
    Posts:
    665
    Plugins:
    2
    Minecraft User:
    HotshotHD
    I think rekk likes chocolate ;)
    RekkuzaRage likes this.
  6. Gamecrafter
    Offline

    Gamecrafter Notable Member Plugin Developer

    Joined:
    Nov 20, 2014
    Posts:
    978
    Plugins:
    9
    Try to look through the PHP manual.
  7. RekkuzaRage
    Offline

    RekkuzaRage Active Member

    Joined:
    Apr 15, 2014
    Posts:
    251
    Minecraft User:
    RekkuzaRage
    Yeah I did, but I understand it thanks to @Falk.
    Hehe, I do. Lol
  8. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,306
    Plugins:
    11
    Minecraft User:
    PEMapModder
    `isset` is not a function. According to php.net, it is a language structure token. According to http://php.net/tokens, isset() is parsed as an individual token (T_ISSET) like return (T_RETURN), public (T_PUBLIC), function (T_FUNCTION), <?php (T_OPEN_TAG), etc, instead of a T_STRING like other functions.

    There are mutliple uses of `isset`:
    1. To check whether a variable is defined and not identical to (!==) null.
      A variable can be defined by:
      1. Normal variable assignment:
        PHP:
        $var "value";
        var_dump(isset($var)); // bool(true)
        $var false;
        var_dump(isset($var)); // bool(true)
        $var null;
        var_dump(isset($var)); // bool(false)
      2. Parameters:
        PHP:
        function myFx($var){
          return isset(
        $var);
        }
        var_dump(myFx("string")); // bool(true)
        var_dump(myFx(false)); // bool(true)
        var_dump(myFx(null)); // bool(false)
      3. extract():
        PHP:
        $array = array("a" => "b""c" => false"e" => null);
        extract($array);
        var_dump(isset($a)); // bool(true)
        var_dump(isset($c)); // bool(true)
        var_dump(isset($e)); // bool(false)
      4. Some other methods that I am not going to mention here, including reference parameter variables passed to functions.
    2. To check whether an array is associated with an element with a given key and the value is not identical to (!==) null.
      PHP:
      $array = array("a" => "b""c" => false"e" => null);
      var_dump(isset($array["a"])); // bool(true)
      var_dump(isset($array["c"])); // bool(true)
      var_dump(isset($array["e"])); // bool(false)
    3. To invoke offsetExists() in an implementation of ArrayAccess (but not offsetGet(), whether offsetExists() returns true or not).
      PHP:
      class MyClass implements \ArrayAccess{
        public function 
      offsetExists($k){
          echo 
      "Invoked offsetExists()"PHP_EOL;
          return 
      true;
        }
        public function 
      offsetGet($k){
          echo 
      "Invoked offsetGet()"PHP_EOL;
          return 
      $k// lol
        
      }
        public function 
      offsetSet($k$v){
          echo 
      "Invoked offsetSet()"PHP_EOL;
        }
        public function 
      offsetUnset($k){
          echo 
      "Invoked offsetUnset()"PHP_EOL;
        }
      }
      $object = new MyClass;
      isset(
      $object["anything"]) // Invoked offsetExists()
    4. To check if a class field is defined and not null and accessible (i.e. private (if you are in that class's context), protected (if you are in that class's or its subclass's context) or public (from all contexts)).
      PHP:
      class MyClass{
        public 
      $pu1$pu2 null$pu3 false;
        private 
      $pr;
        public function 
      issetTest(){
          return isset(
      $this->pr);
        }
      }
      $mc = new MyClass;
      var_dump(
        isset(
      $mc->pu1), // bool(false)
        
      isset($mc->pu2), // bool(false)
        
      isset($mc->pu3), // bool(true)
        
      isset($mc->pr), // bool(false)
        
      $mc->issetTest() // bool(true)
      );
      Note that no undefined/inaccessible warnings are thrown inside the context of isset().
      Another note is that although both $mc->pu1 (no initializer) and $mc->pu2 (initialized as null) return false in isset() without any warnings, an undefined field E_NOTICE level error is thrown if $mc->pu1 is accessed directly, while $mc->pu2 doesn't.
      Also, __isset() would be triggered instead if the class implements it.
    When getting more used to PHP, developers often forget that `$null = null; return isset($null);` returns false. So keep that in mind.

    Note that in PocketMine plugins, number 3 is rarely used except in NBT. For number 1, you don't use it unless you are more skilled with controlloing code flow. For number 2, it is very frequently used, especially in handling commands (checking whether an argument is given).
    Last edited: May 5, 2015
    RekkuzaRage likes this.

Share This Page

Advertisement